Wednesday, May 21, 2008

ADO.NET Entity Framework - Part 2

Working with ADO.NET Entity framework

After looking into the architecture of the Entity Framework, this article I will show the how to create an EDM using the Entity Data Model Wizard.

The Entity Framework compiles a set of conceptual and storage schemas, together with the mappings between them, into bidirectional pairs of Entity SQL statements called client views. These views drive query and update processing in the runtime engine.

Generating an Entity Data model

To generate an EDM you need to first add an ADO.NET Entity Data Model item template to the project. Follow the below given steps to add and EDM to the project

  1. Right-click the project, point to Add, and then click New Item.
  2. Select ADO.NET Entity Data Model in the Templates pane.
  3. Click Add after you enter the name for EDM. This will present the Entity Data Model Wizard.
  4. Select Generate from database in the Choose Model Contents dialog box. Then click Next.
  5. Click the New Connection button. The Connection Properties dialog box is displayed.
  6. Enter your server name, select the authentication method.
  7. Select the database name, and then click OK.
  8. The Choose Your Data Connections dialog box is updated with your database connection settings.
  9. Select all the tables from the dialog box and click Finish.

The Entity Framework tools generate a class derived from ObjectContext that represents the entity container defined in the conceptual model. The ObjectContext class supports queries against an EDM that return entities as objects, as well as creating, updating, and deleting entity objects. The Entity Framework supports object queries against an EDM. Queries can be composed using Entity SQL, LINQ to entities and object query builder methods.

Once the entity data model is generated you can use any of the above given methods to query your ObjectContext class.

For example :

using (BlogEntities entities = new BlogEntities())


var query = from blogentry in entities.blogentries


orderby blogentry.datepublished descending

select blogentry;

return query;


No comments: