Entity Framework 5.0 & Upgrading Project to FW 4.5

September 25, 2012

EF5, Entity Framework, NuGet

I was listening to a recent .NET Rocks with Julie Lerman on Entity Framework 5, and she was asked if EF5 worked with both .NET 4.5 and .NET 4.0.

Around minute 26, she briefly mentioned it’s tricky because the dll that gets installed via NuGet is determined by the Target Framework the project is set to at the time you install EF via NuGet.

I ran across an easy way to tell today & wanted to share, while elaborating on what she said with pretty pictures =D.

NuGet writes a packages.config file in the root directory of every project a NuGet package has been installed in. Open it and you can quickly tell which version was targeted.

For example, my current .NET Framework 4.5 project, shows this:

image

When I create a .NET Framework 4.0 project & install EF5, I get this:

image

Here’s the Gotcha:

When I changed the Target Framework of my 4.0 project to 4.5, THIS DID NOT CHANGE.

Another important part that DID NOT CHANGE when I changed the target Framework was the section config:

image

The top one shows the App.config for the project I installed EF into that was set to target 4.5 when I installed the NuGet package.

The bottom one shows the App.config for the project I installed EF into that was set to target 4.0 when I installed the NuGet package AFTER I changed the Target framework to 4.5.

If you look at the EntityFramework Reference properties, you’ll see it actually installs different versions:

image image

…;and not to beat a dead horse, but if you then look at the downloaded NuGet package in your solution root\packages\EntityFramework.5.0.0\lib, you’ll see there are completely separate dlls that get installed, depending on your Target Framework at the time of installation,

image

My Recommendation

Sure, there is probably some “change this and this and this and copy this and change this” manual way of modifying the settings & EF dll if you upgrade your framework, but do yourself a favor:

  1. Check everything into your Git branch
  2. “Manage NuGet Packages..” –> Uninstall EntityFramework
    (or Uninstall-Package EntityFramework in the Package Manager Console) &
  3. Reinstall EntityFramework
    (or Install-Package EntityFramework in the Package Manager Console)

You will thank yourself in the morning :-)

4 Comments on “Entity Framework 5.0 & Upgrading Project to FW 4.5”

  1. tpaksu Says:

    OK, but how can I upgrade my EF4.0 to EF4.4 on my existing projects? I’m adding the EntityFramework reference to my project, will it then use the newer one instead my old System.Data.Entity?

    Reply

  2. Vinja Says:

    if I change project version to .net 4.5 (from .net 4.0) will my EF I am using in that project automatically upgraded to 5.0 or i still need download EntityFramework.dll ? The thing i’ve never used
    DbContext, my project only uses ObjectContext

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: