Upgrading to Entity Framework 4.1 RC | John Papa

John Papa

Evangelist on the loose

Upgrading to Entity Framework 4.1 RC

...

Entity Framework 4.1 RC is out and the team has a great post on what’s included and how to get the bits. I am working on a project using them so I upgraded my code and found a few things that I thought I’d pass along as some tips. Some are more obvious, others might be more special cases that I hit. But either way, it was pretty simple. The entire process took me about 20 minutes, and I was able to walk my colleagues through it in about 5 minutes (typing over Skype).

 

Upgrading to EF 4.1 RC – 10 Second Overview

  1. Typed uninstall-package EFCodeFirst in the NuGet console
  2. Typed install-package EntityFramework in the NuGet console
  3. Updated a few lines of code for some breaking changes from CTP 5 to RC

1. Uninstall Entity Framework 4.1 CTP 5

First, grab NuGet from here.

  1. The first step is to open the NuGet Package Manager Console by opening the menu:
    • View –> Other Windows –> Package Manager Console
  2. Make sure the correct project is selected in the drop down (mine is AccoutnsAtAGlance.Model, shown in the figure below)
  3. Type uninstall-package EFCodeFirst and click ENTER

Alternatively, you could use the UI to uninstall NuGet packages. Here are the alternate steps:

  1. Right click your project and select Add Library Package Reference
  2. Select Installed Packages in the left menu
  3. Click the Uninstall button next to the EFCodeFirst package

SNAGHTML545f5af

2. Install Entity Framework 4.1 RC via NuGet

You can install EF 4.1 RC via a regular download installer, but NuGet is so easy.

  1. The first step is to open the NuGet Package Manager Console by opening the menu:
    • View –> Other Windows –> Package Manager Console 
  2. Make sure the correct project is selected in the drop down (mine is AccoutnsAtAGlance.Model, shown in the figure below)
  3. Type install-package EntityFramework and click ENTER image

Alternatively, you could use the UI to add NuGet packages. Here are the alternate steps:

  1. Right click your project and select Add Library Package Reference
  2. Select Online in the left menu
  3. Type EntityFramework in the search box
  4. Click the Install button

SNAGHTML538d0c1

3. Tips

  • TIP 1 – I use Resharper and for some reason Resharper did not keep up with the changes. It the new API changes in red, even though the project was building. To resolve this you can either force Resharper to refresh its cache or the easiest thing to do is to close your solution and reopen it.
  • TIP 2 – Where I invoke Stored Procedures (which I admit is rare) I had to change the call from the commented out code to the code below it. The SqlCommand method was renamed ExecuteSqlCommand, which is more of a verb anyway Smile
//return base.Database.SqlCommand("DeleteAccounts");     
return base.Database.ExecuteSqlCommand("DeleteAccounts");

  • TIP 3 – The ModelBuilder object was renamed to DbModelBuilder. So this code changed, too:
    //protected override void OnModelCreating(ModelBuilder modelBuilder)
    protected override void OnModelCreating(DbModelBuilder modelBuilder)

  • TIP 4 – The extension methods MapLeftKey and MapRightKey also changed their overloads. So I had to change this many to many mapping from the commented out code to the code below it. Basically, the lambdas were pretty redundant in CTP 5.
//modelBuilder.Entity<WatchList>().HasMany(w => w.Securities)    
// .WithMany()
// .Map(map => map.ToTable("WatchListSecurity")
// .MapRightKey(s => s.Id, "SecurityId")
// .MapLeftKey(wl => wl.Id, "WatchListId"));
modelBuilder.Entity<WatchList>().HasMany(w => w.Securities)
.WithMany()
.Map(map => map.ToTable("WatchListSecurity")
.MapRightKey("SecurityId")
.MapLeftKey("WatchListId"));

  • TIP 5 – This one was weird, I admit. For some reason I had to map a model named Security to the table Securities. In CTP 5 this was not necessary, but in the RC I was receiving an error about the mapping for the model Security was not found. Once I added this, all was well. I’ve brought this to the team’s attention as I hope its just something I am doing wrong. If so, I will update this post to clarify it based on what the EF team says.
modelBuilder.Entity<Security>().ToTable("Securities");

  • TIP 6 – This is an API change for the better. The ProxyCreationEnabled property is now a member on the DbContext.Configuration object, so the code went from the commented out code to the code below it. In CTP 5 I had to cast the DbContext variable to IObectContextAdapter and then use the ObjectContext.ConextOptions to get to the property I wanted. Yuck. In EF 4.1 RC I can access both the ProxyCreationEnabled and LazyLoadingEnabled properties off the DbContext.Configuration object.
//return ((IObjectContextAdapter) _DataContext)
//.ObjectContext.ContextOptions.ProxyCreationEnabled = false;
return _DataContext.Configuration.ProxyCreationEnabled;

tags: Silverlight
  • http://www.grb2012.com Anonymous

    I’ve been using ctp5. But couldn’t uninstal with NuGet. Package wasn’t listed in the Add library screen either?
    PM> Uninstall-Package EFCodeFirst
    Uninstall-Package : Unable to find package ‘EFCodeFirst’.
    So I just did a Nu-Get install-package EntityFramework which appeared to work as I had to change my using statements etc. to reflect the changes in the RC. Project builds and works.
    in this article blogs.msdn.com/…/ef-4-1-release-
    I found this:
    "ADO.NET Entity Framework 4.1 RC is available in a couple of places:
    •Download the stand alone installer
    •Add the ‘EntityFramework’ NuGet package to your project
    Note: The NuGet package only includes the EF 4.1 runtime and does not include the Visual Studio item templates for using DbContext with Model First and Database First development."
    I’m using CodeFirst so figured I might want those templates. Installed this to after reading release notes which didn’t mention anything about removing packages… This installed completed successfully and my application still built and ran.
    The updated did cause a drop and recreate in my test database :-)

  • http://tdryan.blogspot.com/ Anonymous

    Hello:
    I too prefer NuGet over the installer; however, installing via NuGet did not include the ‘ADO.NET DbContext Generator.’ That forces me to install using the installer.
    Just an FYI for those expecting to find the ‘ADO.NET DbContext Generator’ as an available Code Generation Item…
    Thanks… -Dan

  • http://www.mockupbuilder.com Anonymous

    definitely worths to try!

  • Anonymous

    The only reason you’d want to uninstall the CTP via NuGet is if you’d installed it via NuGet. If your project predates the existence of the NuGet packages as mine does, then if you try to use NuGet to uninstall it, it wont work. Also, the NuGet packages for SqlCE 4 with Code First have a dependency on Web Activator. If your project is a WPF or WinForms app using SqlCE 4, you probably installed it manually to avoid adding the Web Activator which really isn’t appropriate in the desktop environment. In that case, attempting to uninstall the CodeFirst CTP via NuGet will fail.

  • Anonymous

    You should also note that the RC turns on validation by default, whereas in CTP 5 that wasn’t the case.
    So, for me, I had some strings that were longer than the default 128 and were failing validation.

  • http://btburnett.com Anonymous

    Also, The DatabaseGenerationOption enumeration, which is used by the DatabaseGenerated attribute, has been renamed to DatabaseGeneratedOption.

  • http://weblogs.asp.net/scottguportuguese/archive/2011/03/20/rc-do-entity-framework-41-inclui-a-biblioteca-ef-fode-first.aspx Anonymous

    Semana passada, a equipe de dados entregou a versão RC – Release Candidate (Candidata a Lançamento) do

  • http://weblogs.asp.net/scottguportuguese/archive/2011/03/20/rc-do-entity-framework-41-inclui-a-biblioteca-ef-code-first.aspx Anonymous

    Semana passada, a equipe de dados entregou a versão RC – Release Candidate (Candidata a Lançamento) do

  • Anonymous

    Understandably, there are more changes to the fluent API than are discussed here.
    For example, IsIndependent() has gone AWOL but I’m not sure where to turn to get the list of all the breaking changes betwen CTP 5 and this release. Suggestions?

  • http://arsalkhatri.wordpress.com/ Anonymous
  • http://www.nimeshdotnet.info Nimesh

    Thanks your suggestion helped me. Just starting with EF and this post was useful.

%d bloggers like this: