ADO.NET Entity Framework and Object Services Tutorial - Entity Data Model

ADO.NET Entity Framework and Object Services Tutorial - Entity Data Model

by David Hayden ( Microsoft MVP C# ), Filed: ADO.NET Entity Framework

 

While I was playing with Linq to SQL the other day, I figured I would also look at the ADO.NET Entity Framework and its Object Services to pull off the same simple example I discussed in the following tutorial:

Linq to SQL Example Tutorial - Visual Studio ORCAS March 2007 CTP - ASP.NET

Things were not quite as easy as there is no visual designer in the Visual Studio ORCAS March 2007 CTP and the wizard to create a model from a database generates long namespaces that the Entity Data Model Custom Tool chokes on. Welcome to the world of CTPs, however. Once you get past those issues you are home free.

Here is my new project using the Entity Data Model approach to creating and persisting a new Subscriber to a database:

 

Entity Data Model Tutorial

 

We started off with a database with a Subscribers Table:

 

Entity Data Model Example

 

and used a wizard that allows one to create a conceptual model based on an existing database. I won't bore you with the XML, but essentially the wizard generates the 3 files shown above in the project:

  • Model1.csdl - Conceptual Model
  • Model1.ssdl - Database Schema
  • Model1.msl - Mapping between Conceptual and Database

 

Once you have these 3 wonderful files, you can go ahead and use the Object Services to persist a new subscriber to the database:

 

using (Entities myDb = new Entities())
{
    Subscribers subscriber = new Subscribers();
    subscriber.Name = "David Hayden";
    subscriber.Email = "emailaddress";

    myDb.AddObject(subscriber);

    int count = myDb.SaveChanges();
}

 

and just like with Linq to SQL, we have the new subscriber in the Subscribers Table:

 

Entity Data Model Object Services

 

Conclusion

The Object Services in the ADO.NET Entity Framework is another solution for persisting entities to your database.

 

Source: David Hayden ( Microsoft MVP C# )

Filed: ADO.NET Entity Framework

 

posted on Monday, March 19, 2007 10:47 PM

Main

News

Green Tea

.NET Development

Enterprise Library

Patterns & Practices