First Look- ADO .NET Entity Framework

These few days I have been playing around with the ADO .NET Entity Framework. To my surprise this framework is comparable to Castle ActiveRecord. The following are some code snippets that I have played around in these few days.

Using the ADO.NET Entity Provider

// named connection points at model files

using (EntityConnection con =
   new EntityConnection("Name=Northwind"))
{
  con.Open();
  // command uses eSQL
  using (EntityCommand cmd = new EntityCommand(
     "SELECT VALUE c FROM Northwind.Customers", con))
  {
    using (EntityDataReader rdr = cmd.ExecuteReader(
            CommandBehavior.SequentialAccess))
    {
        // DataReader has a variety of shapes
        // process DataReader here
    }
  }
}

Using Object Services

using (ObjectContext ctx = new      
  ObjectContext("Name=Northwind"))
{
  ObjectQuery<Customers> query1 = 
    ctx.CreateQuery<Customers>(
    "select value c from Northwind.Customers as c");

  // query executes here
  ObjectResult<Customers> result = query1.Execute(
     MergeOption.NoTracking);

  ObjectQuery<Customers> query2 = 
    ctx.CreateQuery<Customers>(
    "select value c from Northwind.Customers as c");

  // query executes here
  foreach (Customers c in query2)
    Console.WriteLine(c.CustomerID);
}

Using LINQ to Entities

Northwind nw = new Northwind()

// select entire entity
   var query2 = from c in nw.Customers
                select c;

   foreach (var cs in query2)
     Console.WriteLine("{0} {1}",
                cs.Customerid, cs.CustomerName);

// simple projection
   var query2 = from c in nw.Customers
                select new {
                  CID=c.CustomerID, CName=c.CompanyName              
                };

   foreach (var cs in query2)
     Console.WriteLine("{0} {1}", cs.CID, cs.CName);

Additional Resources

An Entity Data Model for Relational Data Part I: Defining the Entity Data Model , Code Magazine
http://www.code-magazine.com/Article.aspx?quickid=990712022

An Entity Data Model for Relational Data Part II: Mapping an Entity Data Model to a Relational Store , Code Magazine
http://www.code-magazine.com/Article.aspx?quickid=990712032

Programming Against the ADO.NET Entity Framework, Code Magazine
http://www.code-magazine.com/Article.aspx?quickid=990712042

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: