ADO.Net Entity Framework
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
- Lets add the northwind database to our project (Refer My post to add a SQLCE database to VS)
- Then add Entity Data Model to project.Lets name the Entity Model as “NWModel”
- On click of Add it will guide us through a wizard and follow the steps by selecting Northwind database.
- When click on finish VS will create the Entity for us.Also will add a reference to System.Data.Entity.
- To check the Field to property mapping ,select Customer entity in data model and on right click we will find Table Mapping .You will find the Scalee Fields of RDBMS to property mapping.
- We Also check the App.config file it will add a a line for connection string.
- Now to check with the business entities lets select in the solution explorer and click on View the class diagram.
So Except NWEntity all other Business Entities are inherited from Entity Object for easier management and coordination among them selves.
- 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.
- private void btnLoadCustomers_Click(object sender, RoutedEventArgs e)
- NWEntity objContext = new NWEntity();
- var customers= objContext.Customers.ToList();
- lstData.ItemsSource = (from c in customers select c.Company_Name);
- private void btnLoadOrders_Click(object sender, RoutedEventArgs e)
- NWEntity objContext = new NWEntity();
- var orders = objContext.Orders.ToList();
- lstData.ItemsSource = (from o in orders select o.Order_ID);
This is how Entity Framework eases the enterprise application development.
Download the code used for this article SourceCode
- Data Aggregation Using Presentation Model in RIA and Silverlight 4
- Performing CRUD on Relational Data (Multiple table) using RIA in SL4
- Data Binding in Silverlight with RIA and Entity Framework – Part 3 (Validating Input Data)
- Data Binding in Silverlight with RIA and Entity Framework – Part 2 (Updating Data)
- Data Binding in Silverlight with RIA and EntityFramework – Part 1 (Displaying Data)
- Step By Step Guide to WCF RIA enabled SL4 application with Entity Framework
- Ingredients for Perfect Web App Recipe , Silverlight 4.0 + WCF RIA Service+ Entity Framework
About Manas Patnaik (51 posts)
My name is Manas Patnaik as my domain name suggests, and I design & write software. I live in Bangalore. I used to live in Berhampur, Orissa. My e-mail address is manas002002 AT gmail.com.
Well first, there’s a day job at Infosys , Bangalore. There, I work as Technology Analyst .Previously I worked at Relyon Softech Ltd as Software Engineer .Professionally , I am working from last 4 years . My strength as architect and developer is in developing fault-tolerant, highly scalable, high quality enterprise systems. Since 2005 I am developing and deploying on .Net platform, with occassional forays in LISP,J2EE.You can find my resume here.
In any remaining free time, I used to be at community sites for posting solution or else you will find me in kitchen .
If there’s any time left over, i share my experience here in this blog,watch movies or else play strategy games (Rise of Nation my favorite).