Search code examples
c#asp.net-mvcentity-framework

TypeLoadException with DbContext


I have 2 projects. The first project is a dll project, the second is a mvc4 website. In the dll I have a entity data model from which I auto generated the DbContext.

In the Global.asax I initialize the db with this line:

System.Data.Entity.Database.SetInitializer( new DropCreateDatabaseAlways<WebConfigEntities>() );

When I start the site I get a TypeLoadException

Server Error in '/' Application.
GenericArguments[0], WebConfigDB.WebConfigEntities, voor System.Data.Entity.IDatabaseInitializer`1[TContext] is in strijd met de beperking van typeparameter TContext.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.TypeLoadException: GenericArguments[0], WebConfigDB.WebConfigEntities, voor System.Data.Entity.IDatabaseInitializer`1[TContext] is in strijd met de beperking van typeparameter TContext.

Source Error:

Line 46: 
Line 47:            BundleTable.Bundles.EnableDefaultBundles();
Line 48:        }
Line 49:    }
Line 50: }


Source File: D:\projecten\MD2400\WebConfig\Global.asax.cs    Line: 48

Stack Trace:

[TypeLoadException: GenericArguments[0], WebConfigDB.WebConfigEntities, voor System.Data.Entity.IDatabaseInitializer`1[TContext] is in strijd met de beperking van typeparameter TContext.]
   WebConfig.MvcApplication.Application_Start() in D:\projecten\MD2400\WebConfig\Global.asax.cs:48

How can I solve this problem?


Solution

  • This type of error usually indicates that different assemblies are somehow referencing different versions of EntityFramework.dll. Make sure that all your projects (and anything else the references assemblies) are using exactly the same version. Also, make sure that you either don't have EntityFramework.dll in the GAC, or if you must have it in the GAC then it is also the same version as is being referenced.

    If you are using an external dependency that was built against a different version and you cannot change this then you may need to setup binding redirects to make sure the CLR resolves the version appropriately.