gpt4 book ai didi

c# - 带有 DbContext 的 TypeLoadException

转载 作者:行者123 更新时间:2023-12-03 19:14:56 26 4
gpt4 key购买 nike

我有 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com