RC of Entity Framework 4.1 (which includes EF Code First)

Last week the data team shipped the Release Candidate of Entity Framework 4.1.  You can learn more about it and download it here.

EF 4.1 includes the new “EF Code First” option that I’ve blogged about several times in the past.  EF Code First provides a really elegant and clean way to work with data, and enables you to do so without requiring a designer or XML mapping file.  Below are links to some tutorials I’ve written in the past about it:

The above tutorials were written against the CTP4 release of EF Code First (and so some APIs might be a little different) – but the concepts and scenarios outlined in them are the same as with the RC.

Go Live License

Last week’s EF 4.1 RC ships with a “go live” license that enables you to use it in production environments.  The final release of EF 4.1 will ship within the next 4 weeks and will be 100% API compatible with the RC release.

Improvements with the RC

The RC includes several improvements and enhancements.  The EF team has a good blog post summarizing the RC changes.  Scott Hanselman also has a nice video interview with the data team that talks more about the release.

One of my favorite improvements introduced with last week’s RC is its support for medium trust security.  This enables you to use EF 4.1 (and code-first) within low-cost ASP.NET shared hosting web environments – without requiring a hoster to install anything to use it.

EF 4.1 also now supports validation with not only code-first scenarios, but also model-first and database-first workflows. 

Upgrading from previous releases

The RC does include a few API tweaks and changes from the prior CTP builds.  Read the release notes that come with the release to get a more detailed listing of the changes.

John Papa also has an excellent Upgrading to EF 4.1 RC blog post that describes the steps he took when upgrading a large project he wrote with the previous CTP5 release.  The work to upgrade is pretty straight forward and easy – use his write-up as a guide on how to quickly update projects of your own.

NuGet Package Rename

One of the changes that the data team made between the CTP5 and RC releases was to rename the NuGet package name from “EFCodeFirst” to “EntityFramework”. They decided to make this change since the EF 4.1 release now includes several additions above and beyond just code first.

If you already have installed the “EFCodeFirst” NuGet package, you’ll want to uninstall it and then install the new “EntityFramework” NuGet package.  John Papa’s blog post details the exact steps on how to do this (it only takes ~20 seconds to do this).

More EF Tutorials

Julie Lerman has created some nice whitepapers and tutorials for MSDN that show using the new EF4 and EF 4.1 feature set.

Click here to find links to read and watch them.


I’m really excited about the EF 4.1 release that will be shipping next month.  It significantly improves the Entity Framework, and makes it even easier and cleaner to work with data inside of .NET. 

You can take advantage of it within all ASP.NET projects (including both Web Forms and MVC), within client projects using Windows Forms and WPF, and within other project types like WCF, Console and Services.  You can use NuGet to easily install it within all of them.

Hope this helps,


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


  • Very good! As a big fan of EF 4.0 this is pretty much needed. Thanks, again!

  • Hi Scott,

    Thanks for the update! Any idea this release sorts the issue in many to many relationships and cascade deleting ? Where currently we have to modify the .edmx file manually now???

  • Nice to see that Microsoft is so committed to EF. Keep up the good work! Hope in the near future to see some enhancements as an impoved designer (multiple windows and annotation support), even better webservice mapping, -supported- query caching, batch queries and enums.

  • Excellent news.
    Would the RTM be supporting SQL Azure.

  • In case of you use Code First with existing database and you change some property (add, remove or change type) the database will be updated loosing all data in that table affected?

  • Code First is like drinking cold water on a hot refreshing, great work thanks for the opportunity to move forward

  • Code First is like drinking cold water on a hot refreshing, great work thanks for the opportunity to move forward

  • @Aneef,

    >>>>>> Any idea this release sorts the issue in many to many relationships and cascade deleting

    Good question - I just sent mail to the data team to check.



  • @Will,

    >>>>>> I think that on the whole the RC is an improvement over CTP5. However, I do not understand why the default naming convention for foreign keys has changed so that it expects column names like Id becomes _Id - not the underscore (see stackoverflow.com/.../ef-4-1-messing-things-up-has-fk-naming-strategy-changed for more details). I know I can rename all of the columns used in foreign key constraints (although I can’t at my current shop as underscores are not allowed by their naming standards) or I can use the Fluent API to try to rename them (not had much success with this yet!) but I still don't understand why it would change between CTP5 and RC, especially as custom conventions are not supported

    I just sent mail to the data team to try and get more information on this. Will post a comment when I hear back from them.



  • @George,

    >>>>>> Would the RTM be supporting SQL Azure.

    Yes - that is fully supported even today with the RC.

    Hope this helps,


  • @Vitor,

    >>>>>> In case of you use Code First with existing database and you change some property (add, remove or change type) the database will be updated loosing all data in that table affected?

    By default no - EF Code First doesn't delete or recreate your database. You can *optionally* configure it to do this - but this isn't the default behavior.

    Hope this helps,


  • This is great news - I loved using the EF code first but had some problems deploying to my host, medium trust support will make everything much smoother.


  • Hello,

    Will there be a new release of RIA services that works with this excellent release of EF code-first ?


  • Hi,

    Will there be a new release of WCF RIA Services that works with this excellent release of Entity Framework code-first ?

  • I've got a table that contains serialized data of a tree structure. There's a field that's set to varchar(max) and one of the records contains 12k of data. CTP5 had no problems loading the data, but the RC throws the error:

    "Invalid attempt to read when no data is present"

    This is the code in question:

    using (var context = GetContext())
    var query = from item in context.Preferences
    where item.UserName == userName
    where item.PrefName == "TreeState"
    select item;

    // Error on this line
    Preference entity = query.FirstOrDefault();
    return entity == null ? null : entity.Value;

    The table structure is as follows:

    Username [varchar(50)]
    PrefName [varchar(50)]
    Value [varchar(max)] Nullable

    Exactly the same code works perfectly in CTP5. So I'm having to continue using CTP5 at the current moment in time.

  • Is there similar functionality to update database schema such as add new column like nhibernate without drop database recreate it?

  • Hey Scott,

    I noticed that in your summary, you did not explicitly say for use with Silverlight. I have had great success with creating Silverlight prototypes using EF CF (CTP) as my model. In addition, I have enjoyed the support for the data annotations.

    Is there a reason that you didn't mention Silverlight?

  • I really love it!...just want to ask if there is any plan to support EF in smart devices or not? (windows phone 5/6/6.5/7 ?)

  • Keep up the nice work! Hope within the close to future to examine some enhancements as an impoved designer

  • @Will,

    >>>>>> According to this post on the EF Forum, social.msdn.microsoft.com/.../362af001-c3b0-4ca5-a224-08f05c8a0790 , string length validation now arbitrarily defaults to 128 characters. I am not sure I like this change as it seems logical to me that string should validate unless it is constrained by a parameter. Any thought from the community?

    I just sent a mail to the team asking about this.



  • Scott, will ever EF support Async IO operations?

  • Any chance we will be seeing support for the sql_variant SQL Server datatype in the near future?

    I've seen some examples of implementing it using complex types in Entity Framework but from what I gather, writing and querying on sql_variant columns are not supported at all.

  • It looks like the release notes left out the name changes from:
    this.Property(x => x.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
    this.Property(x => x.Id).HasDatabaseGenerationOption(DatabaseGenerationOption.Identity);
    between CTP5 and RC1.

  • oops, got that backwards - went from:
    this.Property(s => s.Id).HasDatabaseGenerationOption(DatabaseGenerationOption.None);
    this.Property(s => s.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);

  • Great news! Using CTP5 in my web with excellents results. Fast development and good performance. Was waiting for medium trust support.

  • Hey Scott, re: Orlando. How about dinner at Christinis? Hoping Dino will join us. See http://www.christinis.com/menu.html

  • Sorry, forgot contact info. gary.campbell@live.ca or gcampbell@canscribe.com - see bottom of http://canscribe.com/about-canscribe/meet-the-canscribe-team/

  • Thanks for update, Scott!

    Does anybody know, how to use precopmiled queries with EfCodefirst? When I'm using ObjectContext from edmx model, it is clear, but what about DbContext?

    Is it possible to use it somehow with CompiledQuery.Compile()?

  • Will this work with Oracle?

  • EF makes a really nice progress. Any chance to see something like NHibernate 2nd level cache in EF realm?

  • I've come across something interesting with this release of EF and SQL Server Compact 4.

    Having a column of the ntext datatype, it seems that EF per default prevents inserts larger than 128 characters. And what i can see there's no possibility to specify "no max length" (actually i thought that was the default when no maxlength attribute was added). So for now it seems i can't insert more than 4000 chars in an ntext field (since maxlengthattribute cant be larger than 4000).

  • What happened to MapHierarchy? I'm trying to build a class hierarchy in my model, but I don't know how to map it.

  • Code First sounds really promising! I may be missing something, but it seems that the "Custom Database Schema Mapping" that worked in the last CTP does not work in RC. When I use a mapping expression like the following:

    map => map.Properties(item => new
    ItemID = item.Id,
    Item = item.Description

    I get this error:

    The properties expression 'item => new f__AnonymousType1`2(ItemID = item.Id, Item = item.Description)' is not valid. The expression should represent a property: C#: 't => t.MyProperty' VB.Net: 'Function(t) t.MyProperty'. When specifying multiple properties use an anonymous type: C#: 't => new { t.MyProperty1, t.MyProperty2 }' VB.Net: 'Function(t) New From { t.MyProperty1, t.MyProperty2 }'.

    This is critical to my application since I have no control over the database naming conventions. Is there another way to accomplish this (without using attributes)?

  • thnx for sharing great article

  • If you are interested in using the Entity Framework with Domain Driven Design, checkout my new "Domain Driver" framework on CodePlex.

    This framework provides developers with a way to quickly and easily implement a full domain model, to test that domain model using pre-built generic tests, and to prototype user-inferface components against that domain model to help achieve customer validation. Initially, the domain model will function as an in-memory database, but at any point developers can add the ability to persist to a database, a file, or any other non-volatile data store.

    Domain Driver itself is decoupled from any persistence technology, but I implemented an example that clearly shows how to use it with EF Code-First to accomplish Database persistence.

  • By the way, today the "micro-ORM" framework that powers StackOverflow was released to opensource - http://code.google.com/p/dapper-dot-net/

  • Nice would need to write a test app soon!

  • I use Framework 3.0 , I try Framework 4

    Thank for Sharing

  • thanks...i had question in my mind...

Comments have been disabled for this content.