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.

Summary

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,

Scott

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

Published Saturday, March 19, 2011 11:32 PM by ScottGu

Comments

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

Sunday, March 20, 2011 2:38 AM by Derya Netter

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

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

Sunday, March 20, 2011 2:40 AM by Aneef

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???

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

Sunday, March 20, 2011 6:03 AM by roger.hendriks@fenetre.nl

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.

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

Sunday, March 20, 2011 8:07 AM by George

Excellent news.

Would the RTM be supporting SQL Azure.

Thanks

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

Sunday, March 20, 2011 8:53 AM by Vitor Canova

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?

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

Sunday, March 20, 2011 2:47 PM by rickj1

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

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

Sunday, March 20, 2011 2:56 PM by rickj1

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

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

Sunday, March 20, 2011 3:54 PM by ScottGu

@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.

Thanks,

Scott

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

Sunday, March 20, 2011 3:55 PM by ScottGu

@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 <AnyOldForeignKey>Id becomes <AnyOldForeignKey>_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.

Thanks,

Scott

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

Sunday, March 20, 2011 3:55 PM by ScottGu

@George,

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

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

Hope this helps,

Scott

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

Sunday, March 20, 2011 4:27 PM by ScottGu

@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,

Scott

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

Sunday, March 20, 2011 11:15 PM by Henry Tait

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.

Thanks

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

Monday, March 21, 2011 3:29 AM by Kakone

Hello,

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

Cordially,

Kakone.

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

Monday, March 21, 2011 6:30 AM by kakone

Hi,

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

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

Monday, March 21, 2011 7:04 AM by Daniel Done

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:

   Preference

   {

       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.

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

Monday, March 21, 2011 8:02 AM by javafun

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

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

Monday, March 21, 2011 11:21 AM by Jeremiah

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?

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

Monday, March 21, 2011 5:57 PM by mahmoud6901

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 ?)

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

Tuesday, March 22, 2011 2:20 AM by benivolent

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

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

Tuesday, March 22, 2011 2:29 AM by ScottGu

@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.

Thanks,

Scott

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

Tuesday, March 22, 2011 5:25 AM by Guchko Gleb

Scott, will ever EF support Async IO operations?

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

Tuesday, March 22, 2011 5:35 AM by Tobias S

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.

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

Tuesday, March 22, 2011 11:12 AM by Danny Fafach

It looks like the release notes left out the name changes from:

           this.Property(x => x.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

to:

           this.Property(x => x.Id).HasDatabaseGenerationOption(DatabaseGenerationOption.Identity);

between CTP5 and RC1.

Thanks,

Danny

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

Tuesday, March 22, 2011 11:16 AM by Danny Fafach

oops, got that backwards - went from:

 this.Property(s => s.Id).HasDatabaseGenerationOption(DatabaseGenerationOption.None);

to:  

 this.Property(s => s.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);

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

Wednesday, March 23, 2011 12:23 PM by Alvaroma

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

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

Thursday, March 24, 2011 2:56 AM by GaryECampbell

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

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

Thursday, March 24, 2011 3:01 AM by GaryECampbell

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

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

Thursday, March 24, 2011 4:45 AM by Alexander Putilin

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()?

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

Thursday, March 24, 2011 7:01 AM by Cristovão Morgado

Will this work with Oracle?

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

Thursday, March 24, 2011 4:10 PM by VasilioRuzanni

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

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

Friday, March 25, 2011 3:14 AM by Martin

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).

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

Monday, March 28, 2011 1:14 PM by Vincent-Philippe Lauzon

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

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

Monday, March 28, 2011 1:22 PM by jrummell

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:

modelBuilder.Entity<Item>().Map(

   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)?

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

Friday, April 1, 2011 5:29 PM by seliora

thnx for sharing great article

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

Monday, April 4, 2011 6:48 PM by ryanse

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.

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

Tuesday, April 5, 2011 8:55 AM by jitbit

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

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

Friday, April 15, 2011 3:56 AM by varunmaggo

Nice would need to write a test app soon!

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

Thursday, April 21, 2011 12:32 AM by dave2011vn

I use Framework 3.0 , I try Framework 4

Thank for Sharing

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

Tuesday, May 3, 2011 2:22 AM by Aneef

Scott,

Thanks for your response. any reply from data team on cascade delete ?

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

Monday, May 9, 2011 7:38 AM by george

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