.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month

Home >> Articles >> .Net Framework >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Jump Start to ADO.Net Entity Framework

Posted By:Manas Patnaik       Posted Date: January 20, 2011    Points: 75    Category: .Net Framework    URL: http://www.dotnetspark.com  

A Introduction to Entity Framework in .Net Framework 4

Well most of our application driven by a relational database and business layer associated with it.The amount of time spent to establish a communication between these two is quite a lot.So here Microsoft introduces a framework for easy data abstraction called Entity Framework (AEF).AEF does wonder with support of LINQ to Entity and here in this post we will have a detailed demonstration.

What Is Entity Framework

It is an abstraction conceptual schema over the logical database schema (Relational database) .It allows us to eradicate the O-R mismatch between RDBMS and the application logic which is common in  data driven applications .(O-R impedance mismatch ,(Details can be found here).

It introduced with .Net Framework 3.5 Sp1 and improved a lot in .Net 4.0.

Jump Start To Entity Framework

In following example i am going to use Nortwind Database (sqlCE) .Northwind is Microsoft sample database for customers and order management. (NorthWind Schema , NorthWind Database SQLCE) .

  1. Lets add the northwind database to our project (Refer My post to add a SQLCE database to VS)
  2. Then add Entity Data Model to project.Lets name the Entity Model as "NWModel"
  3. www.manaspatnaik.com
  4. On click of Add it will guide us through a wizard and follow the steps by selecting Northwind database.
  5. www.manaspatnaik.comwww.manaspatnaik.comwww.manaspatnaik.com
  6. When click on finish VS will create the Entity for us.Also will add a reference to System.Data.Entity.
  7. To check the Field to property mapping ,select Customer entity in data model and on right click we will find Table Mappingimage .You will find the Scalee Fields of RDBMS to property mapping.www.manaspatnaik.com
  8. We Also check the App.config file it will add a a line for connection string.
  9. Now to check with the business entities lets select www.manaspatnaik.com in the solution explorer and click on View the class diagram.
    www.manaspatnaik.comSo Except NWEntity all other Business Entities are inherited from Entity Object for easier management and coordination among them selves.
  10. Lets go to the code and check how it work to restive data .NwEntity is a wrapper around the entities  and can be used retrieve data related to entities.Have a look at the NwEntity Class bellow.

So In our current application we need to load all the customers and orders on their respective load click.And we can achieve it with a very small piece of code as bellow.

Code Snippet
  1. private void btnLoadCustomers_Click(object sender, RoutedEventArgs e)
  2. {
  3. NWEntity objContext = new NWEntity();
  4. var customers=  objContext.Customers.ToList();
  5. lstData.ItemsSource = (from c in customers select c.Company_Name);
  6. }
  7. private void btnLoadOrders_Click(object sender, RoutedEventArgs e)
  8. {
  9. NWEntity objContext = new NWEntity();
  10. var orders = objContext.Orders.ToList();
  11. lstData.ItemsSource = (from o in orders select o.Order_ID);
  12. }


This is how Entity Framework eases the enterprise application development.


Download the code used for this article SourceCode

 Subscribe to Articles


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend