New Features in ADO.NET Entity Framework 4.0

An overview of what's new in the latest version of Microsoft's Entity Framework

The ADO.NET Entity Framework is an extended Object Relational Mapping (ORM) solution from Microsoft that abstracts the object model of an application from the relational model and in doing so, reduces the impedance mismatch that exists among these models. The Entity Framework provides additional features over and above an ORM solution. Among these features are support for entity inheritance, entity composition, and change tracking. Microsoft has introduced many new features and enhancements in Entity Framework 4.0. This article takes a look at these features and enhancements and discusses how you can leverage these features in your applications.

Entity Framework Overview
The core of the Entity Framework is the Entity Data Model (EDM). The EDM allows you to work with domain-specific properties rather than having to be concerned about how the data is actually represented and stored in the underlying database.

New Features and Enhancements in ADO.NET Entity Framework 4.0
ADO.NET Entity Framework 4.0 ships with Visual Studio 2010 and includes the following new features and enhancements:

  • persistence ignorance
  • lazy loading
  • self-tracking entities
  • POCO change tracking
  • model-first development
  • built-in functions and model-defined functions

Persistence ignorance. This is a concept that enables you to design and implement applications that are independent of the persistent technology. You can now create your own Plain Old CLR Objects (commonly known as POCO) that are decoupled from any specific persistence technology. The following is a typical example of a POCO entity:

 

public class Product
    \\{
        public int ProductID \\{ get; set; \\}
        public string ProductName \\{ get; set; \\}
        public int SupplierID \\{ get; set; \\}
        public string QuantityPerUnit \\{ get; set; \\}
        public decimal UnitPrice \\{ get; set; \\}
        public bool Discontinued \\{ get; set; \\}
        public Category Category \\{ get; set; \\}
    \\}

Support for lazy loading. Entity Framework provides support for lazy loading of entities—which means, in essence, that the entities are loaded on demand. Entity Framework 4.0 provides enhanced support for lazy loading. To enable lazy loading, you need to set the LazyLoadingEnabled property to true, as shown in the following code snippet:

dataContext.ContextOptions. DeferredLoadingEnabled=true;

Support for self-tracking entities. Entity Framework 4.0 provides excellent support for self-tracking entities, which are entities that can track their changes, so that they can be passed across process boundaries and the complete object graph persisted. The Entity Framework Design Team states: "Self-tracking entities know how to do their own change tracking regardless of which tier those changes are made on. As an architecture, self-tracking entities falls between DTOs and DataSets and includes some of the benefits of each." (Reference: http://blogs.msdn.com/b/efdesign/archive/2009/03/24/self-tracking-entities-in-the-entity-framework.aspx)



Support for model-first development. You can now use Entity Framework 4.0 to create your object model and then generate the relational model from your object model. To do this, you first need to design your object model using the Entity Data Model in the Visual Studio 2010 IDE, then generate the relational model based on this object model. This feature is known as model-first development. Model-first or code-first development is a great new feature in Entity Framework 4.0 that enables you to define your model objects by simply using plain old classes without the need of base classes for such entities. Note that model-first development would generate the necessary SQL scripts for all entities. You can learn more about model-first development from these resources:

Support for built-in functions and model-defined functions. You can now use built-in SQL Server functions directly in your queries, as the following code snippet demonstrates:

 

from s in MyDataContext.Sales
where new\\[\\] \\{"january","february","march",”april”\\}.Contains(SqlFunctions.DateName("month", s.SalesDate))
orderby s.SalesmanID
select new
\\{
 s.SalesmanID,s.SalesDate
\\};

You can now also use model-defined functions in Entity Framework 4.0. Model-defined functions are those that can be defined in the CSDL. The following is an example of how such functions can be implemented:

<Function Name="GetStudentAge" ReturnType="Edm.Int32">
      <Parameter Name="Student" Type="Self. Student" />
        <DefiningExpression>
          Edm.DiffYears(Student.BirthDate, Edm.CurrentDateTime())
        </DefiningExpression>
</Function>


Suggested Readings
Here are the links to some references on this topic for further study:

DevProConnections articles:

Renovations to .NET 4.0's Entity Framework

Entity Framework 4, Beta 2 Is Here

Other Resources

http://www.develop.com/entityframework4
http://code-magazine.com/Article.aspx?quickid=0909081
http://blog.tonysneed.com/2009/07/09/top-ten-new-features-in-entity-framework-4-0/
http://aleembawany.com/2009/05/17/new-features-in-entity-framework-40-v2/
http://geekswithblogs.net/iupdateable/archive/2009/09/23/entity-framework-4.0-resources-ndash-documentation-links-best-blog-posts.aspx
http://msdn.microsoft.com/en-us/library/ff407090.aspx
http://www.amazon.com/Entity-Framework-Tutorial-Joydip-Kanjilal/dp/1847195229/


Joydip Kanjilal ([email protected]) is a Microsoft MVP in ASP.NET. He has more than 12 years of industry experience in IT with more than six years in Microsoft .NET and its related technologies. Joydip blogs at http://aspadvice.com/blogs/joydip.

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish