[英]Error When running Update-Database using Entity Framework, Code First (POCO) and Code Contracts
I have a project that is using Entity Framework 6
with a POCO code first approach
. 我有一个项目使用
Entity Framework 6
与POCO code first approach
。 I have a set of class libraries that i have included in my project to allow for code reuse. 我有一组类库,我已经包含在我的项目中以允许代码重用。 These libraries utilize Code Contracts.
这些库使用代码契约。
I have also enabled Static and Runtime
checking for my start-up project. 我还为启动项目启用了静态和
Runtime
检查。
The project builds fine and executes correctly. 项目构建正常并正确执行。 The database is created successfully when I execute "Update-Database".
执行“Update-Database”时,数据库成功创建。
My problem arises when I try to call a method in an external library that uses code contracts during the Seeding of the database. 当我尝试在数据库的Seeding期间使用代码契约的外部库中调用方法时,我的问题出现了。 If I omit these functions it seeds correctly.
如果我省略这些功能,它会正确播种。 If I include a function that has code contract then I get the following when I execute the "Update-Database".
如果我包含一个具有代码契约的函数,那么当我执行“Update-Database”时,我会得到以下内容。 It is important to note that the
SQL
to generate the table structures executes correctly. 请务必注意,生成表结构的
SQL
可以正确执行。 This only affects the SQL
for seeding. 这仅影响用于种子设定的
SQL
。
Running Seed method.
运行种子方法。 System.Runtime.Serialization.SerializationException: Type is not resolved for member 'System.Diagnostics.Contracts._ ContractsRuntime+ContractException+ContractExceptionData,WebForms, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
System.Runtime.Serialization.SerializationException:成员'System.Diagnostics.Contracts._ ContractsRuntime + ContractException + ContractExceptionData,WebForms,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null'的类型未解析。 at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
在System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
at System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner runner) at System.Data.Entity.Migrations.Design.ToolingFacade.Update(String targetMigration, Boolean force) at System.Data.Entity.Migrations.UpdateDatabaseCommand.<>c _DisplayClass2.<.ctor>b_ 0() at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command) Type is not resolved for member 'System.Diagnostics.Contracts.在System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner runner)的System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner runner)中,System.Data.Entity.Migrations.UpdateDatabaseCommand上的System.Data.Entity.Migrations.Design.ToolingFacade.Update(String targetMigration,Boolean force)。 System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(操作命令) <> c _DisplayClass2。<。ctor> b_ 0()已解析成员'System.Diagnostics.Contracts的类型。 _ContractsRuntime+ContractException+ContractExceptionData,WebForms, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
_ContractsRuntime + ContractException + ContractExceptionData,WebForms,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null'。
I've managed not exactly a fix for this, but at least been able to work out how to isolate the problem. 我没有完全解决这个问题,但至少能够找出解决问题的方法。
Obviously one of your external methods is raising a ContractException
and this doesn't report usefully to the console, probably due to its being an internal
class and non-constructible across the AppDomain
boundary? 显然你的一个外部方法是引发一个
ContractException
,这并没有对控制台有用的报告,可能是因为它是一个internal
类,并且在AppDomain
边界不可构造?
Assuming you can, go to the project properties and in the Code Contracts
tab, turn off the Perform runtime contract checking
. 假设您可以转到项目属性,并在“
Code Contracts
选项卡中,关闭“ Perform runtime contract checking
。 For me this highlighted the problem immediately, as the migrations code threw up an ArgumentException
with the actual detail. 对我来说,这立即突出了问题,因为迁移代码抛出了带有实际细节的
ArgumentException
。
If this doesn't show the problem, then you'll need to work out where the Contract
is most probably failing and replace each ContractException
with a 'normal' exception and retest until that 'normal' exception is thrown and you work out which contract is failing. 如果这没有显示问题,那么你需要找出
Contract
最有可能失败的地方,并用“正常”异常替换每个ContractException
并重新测试,直到抛出“正常”异常并找出合同失败了。
Whether you then have runtime contract checking on for this assembly is a question. 然后,您是否对此程序集进行运行时协定检查是一个问题。 You have three options:
你有三个选择:
Design your code to ensure that none of the Contract.Requires
(which I'm assuming it is) can fail at runtime (which is what it should be in theory) and not worry about it. 设计你的代码以确保
Contract.Requires
(我假设它)都不会在运行时失败(理论上它应该是这样)并且不用担心它。 You'll then have some difficulty debugging any problems that may occur for any cases you've missed out. 然后,在调试任何您错过的案例时可能会遇到的任何问题都会遇到一些困难。
Turn off runtime-checking. 关闭运行时检查。 This way you'll get a 'normal' exception (though in theory there shouldn't be any in the first place).
通过这种方式,您将获得“正常”异常(尽管理论上不应该首先出现异常)。 Some people like to do this, others don't.
有些人喜欢这样做,有些则不喜欢。 I prefer to keep them.
我宁愿保留它们。
Isolate any code that can't handle ContractExceptions
into its own assembly and turn off runtime contract checking for this. 将任何无法处理
ContractExceptions
代码隔离到自己的程序集中,并为此关闭运行时协定检查。 Not necessarily ideal, but it would allow you to keep runtime checking for the rest of the code. 不一定理想,但它允许您保持运行时检查其余代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.