ADO.NET Entity Framework enables developers to work directly with object model of a database instead of writing SQL queries. Developers which already worked with LINQ to SQL will easily switch to Entity Framework, because the overall using of it is pretty similar.
Ok, let's move on to the example. I created a WPF project with a datagrid in the main window and a simple local database with one single table Employees. Now I want to use ADO.NET Entity Framework to fetch data from the database and show it in my datagrid. Final result will look like this:
In order to add Entity Framework to your application mouse right click on the project then "Add New Item", choose ADO.NET Entity Data Model:
Click "Next", then choose desired tables or/and stored procedures. In my case I choose my Employee table, click "Finish":
That's all! You now have Entity Framework Model. Here how you use it in the code:
public List<Employee> GetData { get { using (DBEntities context = new DBEntities()) { var query = from e in context.Employees select e; return query.ToList<Employee>(); } } }Download the source code of this example (Visual Studio 2010 project).
No comments:
Post a Comment