Object relational designer is used to create entities.Lets create a Northwind database Object Model and query it through LINQ.I am using ADO.net Entity framework wizard for quick start.
On solution right click, Add new item.
Go to Template and select ADO.net Entity Framework Model.
in next step , choose connection to datasource and datasource.
- click on new Connection
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
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 ADO.net entity framework.
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)
{
listBox1.Items.Add(c.CategoryName);
}
}
Conclusion. ADO.net entity together with LINQ saves greater time to do same thing by creating objects manually and doing a lot of coding.
Excellent Post. Also visit http://www.msnetframework.com/
ReplyDelete