Calypoter
Calypoter

Reputation: 155

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?

Upvotes: 2

Views: 2494

Answers (3)

midohioboarder
midohioboarder

Reputation: 480

I made the mistake of getting the version number from the solution explorer of the EF dll not the version of the nuget package. So, I loaded nuget package version 6.0.0 instead of 6.2.0.

Beware that these version are not the same thing

Upvotes: 0

Arthur Vickers
Arthur Vickers

Reputation: 7523

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.

Upvotes: 6

mfussenegger
mfussenegger

Reputation: 3971

Translation of the exception would help. Does specifying the Context in the generic method call help? Like this:

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

How are you passing the connectionString to the WebConfigEntities class? Could you post the constructor of it?

EntityFramework does some magic to retrieve the connectionString from the .config file. Maybe that doesn't work well if you have the model in a separate dll. That doesn't mean that you can't have the model in a dll, you can of course. I've done that for several projects.

Upvotes: 0

Related Questions