Entity Framework 6: Alpha2 Now Available

The Entity Framework team recently announced the 2nd alpha release of EF6.   The alpha 2 package is available for download from NuGet. Since this is a pre-release package make sure to select “Include Prereleases” in the NuGet package manager, or execute the following from the package manager console to install it:

PM> Install-Package EntityFramework -Pre

This week’s alpha release includes a bunch of great improvements in the following areas:

  • Async language support is now available for queries and updates when running on .NET 4.5.
  • Custom conventions now provide the ability to override the default conventions that Code First uses for mapping types, properties, etc. to your database.
  • Multi-tenant migrations allow the same database to be used by multiple contexts with full Code First Migrations support for independently evolving the model backing each context.
  • Using Enumerable.Contains in a LINQ query is now handled much more efficiently by EF and the SQL Server provider resulting greatly improved performance.
  • All features of EF6 (except async) are available on both .NET 4 and .NET 4.5. This includes support for enums and spatial types and the performance improvements that were previously only available when using .NET 4.5.
  • Start-up time for many large models has been dramatically improved thanks to improved view generation performance.

Below are some additional details about a few of the improvements above:

Async Support

.NET 4.5 introduced the Task-Based Asynchronous Pattern that uses the async and await keywords to help make writing asynchronous code easier. EF 6 now supports this pattern. This is great for ASP.NET applications as database calls made through EF can now be processed asynchronously – avoiding any blocking of worker threads. This can increase scalability on the server by allowing more requests to be processed while waiting for the database to respond.

The following code shows an MVC controller that is querying a database for a list of location entities:

    public class HomeController : Controller

    {

        LocationContext db = new LocationContext();

 

        public async Task<ActionResult> Index()

        {

            var locations = await db.Locations.ToListAsync();

 

            return View(locations);

        }

    }

Notice above the call to the new ToListAsync method with the await keyword. When the web server reaches this code it initiates the database request, but rather than blocking while waiting for the results to come back, the thread that is processing the request returns to the thread pool, allowing ASP.NET to process another incoming request with the same thread. In other words, a thread is only consumed when there is actual processing work to do, allowing the web server to handle more concurrent requests with the same resources.

A more detailed walkthrough covering async in EF is available with additional information and examples. Also a walkthrough is available showing how to use async in an ASP.NET MVC application.

Custom Conventions

When working with EF Code First, the default behavior is to map .NET classes to tables using a set of conventions baked into EF. For example, Code First will detect properties that end with “ID” and configure them automatically as primary keys.

However, sometimes you cannot or do not want to follow those conventions and would rather provide your own. For example, maybe your primary key properties all end in “Key” instead of “Id”. Custom conventions allow the default conventions to be overridden or new conventions to be added so that Code First can map by convention using whatever rules make sense for your project.

The following code demonstrates using custom conventions to set the precision of all decimals to 5. As with other Code First configuration, this code is placed in the OnModelCreating method which is overridden on your derived DbContext class:

        protected override void OnModelCreating(DbModelBuilder modelBuilder)

        {

            modelBuilder.Properties<decimal>()

                .Configure(x => x.HasPrecision(5));

 

        }

But what if there are a couple of places where a decimal property should have a different precision? Just as with all the existing Code First conventions, this new convention can be overridden for a particular property simply by explicitly configuring that property using either the fluent API or a data annotation.

A more detailed description of custom code first conventions is available here.

Community Involvement

I blogged a while ago about EF being released under an open source license.  Since then a number of community members have made contributions and these are included in EF6 alpha 2.

Two examples of community contributions are:

  • AlirezaHaghshenas contributed a change that increases the startup performance of EF for larger models by improving the performance of view generation. The change means that it is less often necessary to use of pre-generated views.
  • UnaiZorrilla contributed the first community feature to EF: the ability to load all Code First configuration classes in an assembly with a single method call like the following:

protected override void OnModelCreating(DbModelBuilder modelBuilder)

{
       modelBuilder.Configurations
           .AddFromAssembly(
typeof(LocationContext).Assembly);

}

This code will find and load all the classes that inherit from EntityTypeConfiguration<T> or ComplexTypeConfiguration<T> in the assembly where LocationContext is defined. This reduces the amount of coupling between the context and Code First configuration classes, and is also a very convenient shortcut for large models.

Other upcoming features coming in EF 6

Lots of information about the development of EF6 can be found on the EF CodePlex site, including a roadmap showing the other features that are planned for EF6.

One of of the nice upcoming features is connection resiliency, which will automate the process of retying database operations on transient failures common in cloud environments and with databases such as the Windows Azure SQL Database.

Another often requested feature that will be included in EF6 is the ability to map stored procedures to query and update operations on entities when using Code First.

Summary

EF6 is the first open source release of Entity Framework being developed in CodePlex. The alpha 2 preview release of EF6 is now available on NuGet, and contains some really great features for you to try.

The EF team are always looking for feedback from developers - especially on the new features such as custom Code First conventions and async support. To provide feedback you can post a comment on the EF6 alpha 2 announcement post, start a discussion or file a bug on the CodePlex site.

Hope this helps,

Scott

P.S. In addition to blogging, I am also now using Twitter for quick updates and to share links. Follow me at: twitter.com/scottgu

Published Tuesday, December 11, 2012 4:10 PM by ScottGu
Filed under: , ,

Comments

# re: Entity Framework 6: Alpha2 Now Available

Tuesday, December 11, 2012 9:24 PM by weitzhandler

Shiat!!!

I got a boner, that's hot news!!!!

YEAH!!

Microsoft's is surely the best programming environment possible ever!

# re: Entity Framework 6: Alpha2 Now Available

Tuesday, December 11, 2012 10:03 PM by RobSwiz

you had me at AWAIT <-- Are you serious, WOW!

# re: Entity Framework 6: Alpha2 Now Available

Tuesday, December 11, 2012 10:09 PM by Erik Johnson

Thanks for all the great work on EF. Any chance someone is looking at the ability to join queries across models? As an ISV, we deliver models in our product, but our ecosystem has additional models that extend our app. It would be great if we could modularize our app and also let our partners and customers use our model as a component. Isn't that what OO is all about?

Also, any chance of making lock hints available in a Linq query?

Thanks, Scott.

# re: Entity Framework 6: Alpha2 Now Available

Wednesday, December 12, 2012 12:20 AM by LS

This is cool but please don't forget L2S!   LS

# re: Entity Framework 6: Alpha2 Now Available

Wednesday, December 12, 2012 2:19 AM by Stefan

For me, I think that "Multi-tenant migrations allow the same database to be used by multiple contexts with full Code First Migrations support for independently evolving the model backing each context." is the greatest news.

I've had problems when working in branches. A new feature branch makes changes to the model, then I have to go back to a release-branch for a bug fix, and "the context has changed"

I take it this will no longer need to be a problem.

# re: Entity Framework 6: Alpha2 Now Available

Wednesday, December 12, 2012 3:05 AM by Joe

Can someone explain to me why executing multiple database queries in parallel would be faster? Harddrive performance is bound by its IOPS running more queries won't be faster it could be even slower?

# re: Entity Framework 6: Alpha2 Now Available

Wednesday, December 12, 2012 3:38 AM by Raj

Does EF6 fix the bug of having two models with the same name in different namespaces not working?

# re: Entity Framework 6: Alpha2 Now Available

Wednesday, December 12, 2012 3:50 AM by Vladimir

Great! But sad that too much attention is put into code-first, instead of improving real life and much more usable database-first approach.

# re: Entity Framework 6: Alpha2 Now Available

Wednesday, December 12, 2012 6:01 AM by Yibo

The Async support feature rocks. No more blocking on UI for large queries then.

# re: Entity Framework 6: Alpha2 Now Available

Wednesday, December 12, 2012 8:48 AM by nan3co

Async Support feature, cool!!!!

# re: Entity Framework 6: Alpha2 Now Available

Thursday, December 13, 2012 4:07 AM by Lee Englestone

Loving the Async.

-- Lee

# re: Entity Framework 6: Alpha2 Now Available

Thursday, December 13, 2012 8:36 AM by meysam

is possible upgrade from EF5?

# re: Entity Framework 6: Alpha2 Now Available

Friday, December 14, 2012 12:37 PM by Pazu

Any chance, that ToListAsync will appear in Linq2Sql as well ? What about Linq2Sharepoint ?

# re: Entity Framework 6: Alpha2 Now Available

Monday, December 17, 2012 11:00 AM by Ryan

@Vladimir - DL the Entity Framework Powertools extension. You can do database-first design and have the tools create your code-first entities/contexts for you from your database. You'll be able to take advantage of most of these cool features even if you don't work code-first anyways.

# re: Entity Framework 6: Alpha2 Now Available

Monday, December 17, 2012 4:55 PM by andriy svyryd

@Joe - Indeed executing multiple database queries in parallel might not be faster than executing them sequentially. If you are referring to the Async feature it is not meant to speed up your application, instead it is supposed to reduce the memory footprint by using less threads to execute the same number of parallel tasks.

# re: Entity Framework 6: Alpha2 Now Available

Tuesday, December 18, 2012 12:46 AM by quantt

Hot news.. I like multi-ternant

# re: Entity Framework 6: Alpha2 Now Available

Tuesday, December 18, 2012 12:11 PM by jackbond

@Joe and @Andriy - The async is client side functionality. Instead of blocking the calling thread on the client for a query that could be running a minute or more on the server, the client thread is released back into the thread pool. It's actually a compiler trick that hides the more familiar (but now antiquated) BeginABC / EndABC asynchronous approach. It is a little strange that they didn't have BeginToList and EndToList all along.

# re: Entity Framework 6: Alpha2 Now Available

Tuesday, December 18, 2012 2:50 PM by glenncondron

@Erik both of the things you are asking for are on our backlog, though neither are being worked on right now. entityframework.codeplex.com/.../266, entityframework.codeplex.com/.../261.

Feel free to vote for them or start a discussion on codeplex if you want to talk about them more with the team.