Reader Level:
ARTICLE

Object class mapping in ASP.Net MVC3 using Entity Framework

Posted by Shirsendu Nandi Articles | ASP.NET MVC April 05, 2011
In this article I will describe how to create Object Class Mapping using Entity Framework.
  • 0
  • 0
  • 7752

Content:

Nowadays most modern business application development projects use object technology such as C# or Java to build application software and relational databases to store the data. But it is not true that we don't have the other options; there are many applications built with procedural languages such as COBOL and many systems will use object databases or XML databases to store data.

So here comes object relational (O/R) Mapping, but for that we need to understand two things: the process of mapping objects to relational databases and how to implement those mappings. In this article the term "mapping" for entity framework in ASP.Net MVC3 will be used to refer to how objects and their relationships are mapped to the tables and relationships between them in a database i.e. SQL Server 2005 and SQL Server 2008.

Now suppose in your ASP.Net MVC3 application you have 2 Model classes:

  1. Route Class

  2. Drop Point Class

The Route class is for trips of a particular distance. For e.g. Mumbai to Delhi is a Route Class.

The DropPoint Class means in that particular (Mumbai Delhi train route) which are the stations.

Now each route has a specified number of drop points. So we have a one-to-many relationship with the Route class and the Drop Point class.

Now here is the Route class definition with Properties:

public class Route

    {

        public int RouteId { get; set; }

        [Required(ErrorMessage = "RouteName is required")]

        public string RouteName { get; set; }

 

         [Required(ErrorMessage = "RouteNumber is required")]

         [StringLength(15, ErrorMessage = "RouteNumber Name Not exceed more than 15 words")]

        public string RouteNumber { get; set; }

 

         #region 1->m Relation between Route and DropPoint

 

         public virtual List<DropPoint> DropPoint_List { get; set; }

         #endregion

 

    }


Now here is the Drop Pont class definition with Properties:

public class DropPoint

    {

        [ScaffoldColumn(false)]

        public int DropPointId { get; set; }

 

        [Required(ErrorMessage = "DropPointName is required")]

        public string DropPointName { get; set; }

 

        public string NewDropPointName { get; set; }

 

        #region m->1Relation between Route

        [DisplayName("Route")]

        public int RouteId { get; set; }

 

        public virtual Route Route { get; set; }

        #endregion

    }

 

Now here the Million dollar question is: in these two classes, how can we make the relationship.

Now in the Route Class you will see we have a list of DropPoint classes. Because for each Route we have multiple DropPoints (1->m relationship).

 

Similarly in the Route class we see that we have a property "public int RouteId { get; set; }". (This actually acts as a foreign key of the Route class).


And we also have a Property of the Class "Route":

 

public virtual Route Route { get; set; }
 

See here we have to give that virtual keyword before the class "route".
 

The reason why we write the property of the class "Route" in here is because class "DropPoint" also has the many to one relationship with Class route.

 

So now we have a relation between two classes. So when we plug these 2 classes with the entity framework there are two classes that will become the two tables with relationships in the database.

 

Now here I have written the code by how you will make these classes with tables by using entity framework. For that you have to add a reference to "entityframework.dll". I have attached the DLL here.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Data.Entity;

using CabAutomationSystem.Models;

 

namespace CabAutomationSystem.DatabaseContext

{

    public class CabDbContext : DbContext

    {

        public DbSet<Route> Route { get; set; }

        public DbSet<DropPoint> DropPoint { get; set; }

       

    }

}


Here the "Dbset" is the database set.

Conclusion:

So in this article we have seen how to map the two classes with object class mapping concept and also with the help of the Entity Framework how to make the two relationship classes with tables.

erver'>
COMMENT USING

Trending up