The Entity Framework in .NET 4 has a lot of new features and enhancements.  We got a lot of great feedback from you on the initial release of the Entity Framework (EF).  Let’s take a look at some of the things coming in new with Entity Framework 4.

Foreign Keys

Entity Framework now includes support for foreign keys.  Foreign key associations allow you to include foreign key properties on your entities, simplifying a number of key scenarios including data binding and n-tier development. Foreign keys can be used to setup entity relationships using foreign key properties directly:

 

    using (BlogEntities ctx = new BlogEntities()) {

        Post myPost = new Post {

            PostID = 102,

            PostName = "Post Title",

            CreatedDate = DateTime.Now,

            PostContent = "Post Content",

            BlogID = 11

        };

        ctx.Posts.AddObject(myPost);

        ctx.SaveChanges();

    }

 

In this example, even though the Blog object with BlogID == 11 was never loaded, we were able to connect our new myPost object to it directly.

 

Lazy Loading Support

Entity Framework now includes support for lazy loading.  When you create a new model in VS2010, entities that offer lazy loading are generated for you.  Lazy loading, which is enabled by default, doesn’t load each object returned by a query until you actually use it.  For instance, lazy loading means that each post in the below snippet isn’t loaded until it’s actually used to print out the post’s PostName property.

 

    using (var ctx = new BlogEntities()) {

        foreach (var b in ctx.Blogs) {

            Console.WriteLine(b.BlogName);

 

            //Note that we don't explicitly load the posts for the current blog,

            //the EF does it 'lazily' for us.

            foreach (var p in b.Posts)

                Console.WriteLine(p.PostName);

        }

 

    }

Plain Old CLR Object Support

EF4 now includes Plain Old CLR Object Support (POCO) support for entities.  This offers better test-driven development and domain-driven design support by allowing you to have no EF dependencies for your entities.  EF will still track changes for POCO entities, allow lazy loading, and will automatically fix up changes to navigation properties and foreign keys.  You can find out more about POCO support in the walkthroughs posted on the ADO.NET blog.

Text Template Transformation Toolkit Code Generation
In the first version of the Entity Framework, code generation didn’t allow for deep customization and wasn’t integrated into Visual Studio.  The Entity Framework now leverages Text Template Transformation Toolkit, or T4, which makes customizing code generation easy, flexible and powerful.  The experience is fully integrated into Visual Studio.  Built-in code generation strategies can be chosen by right clicking on the Entity Framework Designer surface and selecting ‘Add Code Generation Item…’:

Add Code Generation 

You aren’t limited to using the code generation strategies that ship in VS 2010; you can now write your own T4 templates or modify the default templates to provide your own code generation experience. 

Better N-Tier Support

An n-tier design allows you to separate data, business logic, and interaction layers to ensure data integrity and promote maintainability at each tier.  The Entity Framework team received a number of requests for improvements on n-tier support.  They’ve taken this feedback and made improvements to the API to allow for n-tier design, as well as a code generation template that generates objects with built in n-tier features, such as change tracking.  The template generates your entities as a set of CLR classes with Windows Communication Foundation (WCF) serialization attributes in place so they can easily be used in conjunction with WCF services. 

Generated SQL Improvements

We’re constantly trying to improve the readability and performance of the SQL we generate.  Numerous simplifications and improvements of the SQL generated when querying using the Entity Framework have been made in EF4.  One such improvement is the removal of some extraneous joins.  Another is the use of database wildcards for WHERE clause string parameters.  For instance, the following LINQ query will translate into a query that uses a WHERE clause with a LIKE statement and the ‘%’ wildcard to search for all Blogs whose BlogName property begins with “Visual Studio”:

 

    var query = from b in ctx.Blogs

                where b.BlogName.StartsWith("Visual Studio")

                select b;   

 

While these changes may seem small, improved SQL generation can result in queries that execute more quickly and put less load on your SQL Servers and network.

Enhanced Stored Procedure Support

Many databases contain stored procedures that perform custom SQL processing.  Entity Framework allows you to create a function in your entity model that calls a stored procedure through the Function Import feature.  The Function Import feature can now detect the columns returned from a stored procedure and create a corresponding complex type. In addition, existing complex types can be updated when a stored procedure definition changes.  Below, the Entity Framework Designer stored procedure wizard steps you through the process of importing your stored procedures as functions:

Add Function Import 

Entity Framework 4 offers these and other new features to increase developer productivity.  Share your thoughts and ideas with the team on the project forum, connect with the Entity Framework team on their team and design blogs, and check out videos and screencasts on Channel 9.

Namaste!