Wednesday, 18 February 2009

ADO.NET Entity Framework Model entity framework model represents an object model that contains classes (LINQ To SQL ) entities, and association between objects.These objects are based & mapped to the datasource we provide.

Object relational designer is used to create entities.Lets create a Northwind database Object Model and query it through LINQ.I am using Entity framework wizard for quick start.
On solution right click, Add new item.
Go to Template and select Entity Framework Model.

Name it with Nortwindsamplemodel.In th wizard Generate from database.

in next step , choose connection to datasource and datasource.
  • click on new Connection
select server name from dropdown list.
choose authentication options.
select database from dropdownlist.(In example i have used Northwindump database which is a replicated database of northwind)
Test connection and press OK
for th System administrator password (sa) i am keeping password in .config file for simpilicty of this tutorial therefore i have select the option in the step given
->Yes include the sensitive data ine connection string.
Now select the database objects you want to include as entities.

Designer creates .edmx file with database entities(tables,views,stored procedure) as object on designer surface and also creates association based on Primary key,Foreign key relationship between tables in database.Every object is mapped exactly to one matching table in database.There is no option to map one object to joined table in entity framework.

Below figure shows Created Entities(Objects based on tables) and associations.

Now our model is complete and we just need to query the data via the model.

We will use LINQ for this purpose.(Note that DataContext is the main class to send and receive the data from database however we dont code it by our self as wizard has done it)
Lets create store and query category object through LINQ.

using (NorthWindDumpEntities NorthwindStore=new NorthWindDumpEntities())
var _categorylist = from _catlist in NorthwindStore.Categories
select _catlist;
foreach (Categories c in _categorylist)


Conclusion. entity together with LINQ saves greater time to do same thing by creating objects manually and doing a lot of coding.

1 comment:

  1. Excellent Post. Also visit