c# - 带有 DbContext 的 TypeLoadException

标签 c# asp.net-mvc entity-framework

我有 2 个项目。第一个项目是一个dll项目,第二个是一个mvc4网站。 在 dll 中,我有一个实体数据模型,我从中自动生成了 DbContext。

在 Global.asax 中,我用这一行初始化数据库:

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

当我启动网站时,我得到一个 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

我该如何解决这个问题?

最佳答案

此类错误通常表示不同的程序集以某种方式引用了不同版本的 EntityFramework.dll。确保您的所有项目(以及引用程序集的任何其他内容)都使用完全相同的版本。此外,请确保您在 GAC 中没有 EntityFramework.dll,或者如果您必须在 GAC 中拥有它,那么它也与被引用的版本相同。

如果您使用的是针对不同版本构建的外部依赖项并且您无法更改它,那么您可能需要设置绑定(bind)重定向以确保 CLR 正确解析版本。

关于c# - 带有 DbContext 的 TypeLoadException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10850119/

相关文章:

c# - 如何在 wpf 中使用 RelayCommand?

c# - 如何在 C# selenium chrome 驱动程序中验证(用户/密码)代理

c# - 在 Entity Framework 中恢复已删除的导航属性

c# - 从 DbSet 获取 DbContext

.net - 如何使用我的数据库使 sql 成员提供程序和 Entity Framework 工作?

entity-framework - 模拟 DbFunctions.Like

c# - 为 SpriteRenderer 添加高亮颜色功能

c# - NAudio-wav的流字节[]使其播放缓慢

asp.net-mvc - 使用 ASP.New MVC 4 Web Api 进行授权过滤器依赖注入(inject)

javascript - 禁用 Incell Kendo 网格中的编辑