Tuesday, October 8, 2013

Entity Framework 5 Code First to SQL expected .NET 4.5

A few days ago one of my colleague ran into a bit of problem when deploying an ASP.NET web site to a production environment.

The web site was a typical N-Tier architecture, with Entity Framework 5 for .NET 4 with Code First as the Data Access Layer.

Everything worked fine on development environment and test environment. However, once deployed to production server, the first smoke test already showed some unexpected result.


The error read "The specified value is not an instance of type 'Edm.Int32' Parameter name: value."

This is pretty much the worst nightmare for any developer -- a fatal error on deployment to production environment!

The web project's target framework was set to .NET 4. Entity Framework 5 was added via NuGet Package Manager. The error suggested that the database schema did not match with the classes of Data Access Layer. We had checked both the source code and database schema between development and production, and they appeared to be identical.

After a bit of Googling, it turned out that certain functionalities of Entity Framework 5 Code First required .NET 4.5 Framework on the server.

The root cause was that some Code First classes had enum type fields mapped to int type column in some of the DB tables. Enum support for EF only applied to .NET 4.5 core library, and not of the downloadable EF 5 NuGet Package for .NET 4.0.

Both development environment and test environment had .NET 4.5 Framework installed (for other projects), which was missing in the production environment.

After installed .NET Framework 4.5 on the production server, everything worked again.

Apparently, no one, including myself, realized that Entity Framework 5 requires .NET Framework 4.5. EF 5 will work on .NET Framework 4.0, but only support the same set of functionality as EF 4.2

It was a lucky day. The issue resolved without changes in source code or Database schema.

Lesson learned, also some appreciation and a cup of hot coffee. ^.^

No comments:

Post a Comment