Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

ADO.NET Entity Framework 4: New and Notable Features

Leverage the new and enhanced features of ADO.NET Entity Framework 4.0 to model at the right level of abstraction.


advertisement

The ADO.NET Entity Framework is an extended object relational mapping (ORM) tool from Microsoft that has become increasingly popular over the past few years and is widely used these days. Microsoft designed the ADO.NET Entity Framework to objectify an application's data and simplify the development of data-aware applications.

In the first article in this series on the ADO.NET Entity Framework, I discussed the basics and how to program against the Entity Data Model. This concluding article, I will discuss the new features in Entity Framework 4.

Entity Framework 4.0 Prerequisites

To work with Entity Framework and the code examples illustrated in this article, you should have the following installed in your system:

  • Visual Studio 2008 with SP1
  • Entity Framework Version 4


Alternatively, you can simply have Visual Studio 2010 installed -- it comes with Entity Framework 4 by default.

Code First Development with Entity Framework 4

Entity Framework 4 supports "code first development," enabling you to use the Entity Framework designer tool to design your object model and then use it to drive your relational or logical model.

Programming the Entity Data Model

You have a few choices for writing queries and executing them against the Entity Data Model, namely Object Services, Entity Client and LINQ.

While Entity Client is the data provider to connect to the Entity Data Model and perform CRUD operations, Entity SQL is a data store-independent derivative of T-SQL. The following code snippet shows how you can use Object Services to query your data:

using (NorthwindEntities dataContext = new NorthwindEntities())
    {
        ObjectQuery<Products> products =
            dataContext.CreateQuery<Products>(
                "SELECT VALUE p FROM Products AS p");
        foreach (Products p in products)
        {
            Console.WriteLine(p.ProductName);
        }
    }

The same query can be re-written using LINQ as shown in the code snippet below:

using (NorthwindEntities dataContext = new NorthwindEntities())
    {
        var result = from p in dataContext.Products select p;

        foreach (Products product in result)
        {
            Console.WriteLine(product.ProductName);
        }
    }

The Entity Client supports only Entity SQL as the query language. This is in contrast to Object Services, which allows you to use even LINQ to write your queries and execute them against the Entity Data Model. The following code snippet illustrates how you can use Entity Client:

using (EntityConnection conn = new 
    EntityConnection("name=TestEntities"))
{
    conn.Open();
    EntityCommand cmd = conn.CreateCommand();
    cmd.CommandText = @"
SELECT VALUE e 
FROM TestEntities.Managers AS e";
    using (DbDataReader reader = 
        cmd.ExecuteReader(
            CommandBehavior.SequentialAccess))
    {
        while (reader.Read())
        {
            Console.WriteLine(
                "Manager Name: {0}", 
                reader["ManagerName"].ToString());
        }
    }
}


Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap