Installing the .NET 4.5 Framework Can Be Problematic

As it turns out, I clearly need to spend more time dealing with .NET’s unique version of “dll hell” because this really bit me. A couple of weeks ago I installed the .NET 4.5 framework on my server. This server hosts almost all of my websites, my brother’s and a couple sites of a friend of mine. In theory, this should have been fine. In reality, it caused major problems.

I installed the framework and checked some of the sites. My blogs were fine but my brother’s was throwing a strange error.

The type initializer for 'System.Data.Entity.ModelConfiguration.Utilities.TypeExtensions' threw an exception.

That’s not good. Some of my sites use Entity Framework (EF), some don’t, so I quickly checked my sites and they were okay. I then opened up the code for his site in Visual Studio 2012 to see if I could reproduce the error and thankfully I could (problems on remote machines can be so hard to solve). You might have a bit of code that looks like this in your DbContext class (which I think you’ll only have if you are using code-first):


protected override void OnModelCreating(System.Data.Entity.ModelConfiguration.ModelBuilder modelBuilder)
{
  //You do configuration stuff here
}

You are going to have to change that to update it to EF 5, which will get installed into your Global Assembly Cache (GAC) when you install the 4.5 framework. After you make the appropriate change, the code will look something like this when you are done:


protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
{
  //You do configuration stuff here
}

Once I did that and redeployed it was all fixed up. Yay!

only you can prevent strong naming

Thanks to MotoWilliams for adding this picture to my life.

My Day Gets Worse

I emailed my buddy and told him to check his sites out. Unfortunately, a) one of his was also broken, b) he didn’t have VS2012 installed yet, c) I didn’t have a copy of the source and d) he was in the middle of making a bunch of changes, so making this fix was going to be a major problem for him. Oh…snap…

As my friend hurriedly made the changes he needed to make to launch the code in a reasonable state (minus what I would have to change), I looked for ways to solve this via configuration. I didn’t have his code but I did have access to his web.config. And since his site was completely down, he didn’t mind if I monkeyed around trying to find a way to tell the framework to stop using what was in the GAC and use what he actually had deployed in the bin folder.

First I tried that assembly redirection stuff in the web.config. That didn’t work at all. I tried it with a number of different version numbers (depending on how you look at the file, you get different answers on what they are) and made no progress.

At the advice of @keyvan, I also tried solving this via the assembly references in the config. Once again, no dice.

That at the advice of @peydroreys, I tried the DEVPATH stuff. That’s pretty cool. I had no idea that this stuff existed. But apparently I couldn’t get that right either (perhaps some of these are PEBKAC errors), so I was still dead in the water.

After several hours of searching, tweaking and frustration, my friend finished his changes and gave me the code. I had it fixed in five minutes.

EF Is Horrible

Actually, I don’t think so. EF is fine. These days I’m using Massive more though @amirrajan is trying to convince me to use Oak. But EF is often going to be better than other alternatives. I liked it more than NHibernate, but that’s more of a lifestyle choice I think. I like it more than hand-rolling my ADO.NET. I’ve done it a lot so I’m pretty fast at it but still, doing most things in EF is faster.

This is a problem with .NET. Granted, as I said before, I should be more up to speed with my framework of choice’s own version of dependency nightmare theatre. Thankfully I haven’t had these problems in a few years (yay!). When I do it always seems like it makes more sense to have the local copy win and not the GAC. But they don’t ask me for my opinion on such things, so whatever.

Takeaway

So what did I learn out of this?

  1. The installation of a new version of the .NET framework is not an innocuous activity. This is the only problem I’ve had, but it caused quite a bit of stress and I lost a fine Sunday afternoon to fighting with the GAC. I need to remember this.
  2. I need to dig into how all this stuff works again. It just won’t do to be less than prepared for this sort of thing.
  3. If you are an EF user, watch out for this. If you are ready to install the 4.5 framework and you have the dev and QA time, go ahead and update the codez. Once you do that it seems to be just fine. But if you choose not to, remember that I warned you :).

Comments

Laury Karin 2012-09-24 03:18:43

Cool stuff. I'll check that before upgrading some of my own sites .NET version to 4.5.

Diego Vega 2012-10-04 11:24:55

Hello Eric,

I work on the Entity Framework Team at Microsoft and I wanted to answer here in your blog comments since Twitter doesn’t seem to be the ideal channel (I can tell after I sent you 7 tweets in a row last night :)). Here are a few observations that might help:

- The API of Code First’s model builder class was renamed from System.Data.Entity.ModelConfiguraiton.ModelBuilder to System.Data.Entity.DbModelBuilder before we released Code First RTM in EF 4.1. So, it seems that you were using an old (and unsupported) CTP version of EF in some of your sites.

- The Code First API lives in EntityFramework.dll which is part of the Entity Framework bits we release in NuGet (see http://nuget.org/packages/EntityFramework), and not one of the core libraries we release as part of the .NET Framework. Given that I cannot think of any way installing .NET 4.5 could have updated this API.

- The EntityFramework.dll assembly that is part of EF’s NuGet package is designed to be bin-deployed alongside your application. We do not recommend putting it in the GAC and in fact we have not shipped an installing package that would automatically put it in the GAC since EF 4.1. There is nothing like this in either .NET 4.5, Visual Studio 2012 or NuGet. If you find an old version of EntityFramework.dll in the GAC, my recommendation is to remove it.

All of this however doesn’t explain why you saw the exception about the type initializer. We would like to hear more about this and see if we can repro. I would be grateful if you and your buddy that also ran into problems can contact netfx45compat at Microsoft dot com about this issue.

Regarding the upgrade, I would absolutely recommend upgrading your applications to EF5, which contains many bug fixes and new features. EF5 is not an in-place update for previous versions of EF. In order to take advantage of it I recommend you install the new NuGet package for EF5 in your project and recompile your application. You might need to make some small adjustments in your code, in particular if you are using the DataAnnotations that have moved to a different namespace.

I would also recommend using .NET 4.5, which contains many improvements across the board and in particular new EF features like Enums and Spatial support and considerable performance improvements.

Hope this helps,
Diego