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
  4. 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
    4. 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
      4. 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 
        4. 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
          5. 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
            • //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;