使用实体框架,代码优先(POCO)和代码合同运行Update-Database时出错

[英]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. I have a set of class libraries that i have included in my project to allow for code reuse. These libraries utilize Code Contracts.

我有一个项目正在使用Entity Framework 6和POCO代码第一种方法。我有一组类库,我已经包含在我的项目中以允许代码重用。这些库使用代码契约。

I have also enabled Static and Runtime checking for my start-up project.

我还为启动项目启用了静态和运行时检查。

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. 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". It is important to note that the SQL to generate the table structures executes correctly. This only affects the SQL for seeding.

当我尝试在数据库的Seeding期间使用代码契约的外部库中调用方法时,我的问题出现了。如果我省略这些功能,它就会正确播种。如果我包含一个具有代码契约的函数,那么当我执行“Update-Database”时,我会得到以下内容。请务必注意,生成表结构的SQL可以正确执行。这仅影响用于种子设定的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'. at 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._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'的类型未解析。在System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner runner)的System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)处于System.Data.Entity.Migrations.Design.ToolingFacade.Update(String targetMigration,Boolean force)at at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action命令)System.Data.Entity.Migrations.UpdateDatabaseCommand。<> c_DisplayClass2。<。ctor> b_0()处理成员'System.Diagnostics.Contracts的类型未解析。 _ContractsRuntime + ContractException + ContractExceptionData,WebForms,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null'。

1 个解决方案

#1


0  

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,这并没有对控制台有用的报告,可能是因为它是一个内部类,并且在AppDomain边界上是不可构造的?

Assuming you can, go to the project properties and in the Code Contracts tab, turn off the 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.

如果这没有显示问题,那么你需要确定合同最有可能失败的地方,并用“正常”异常替换每个ContractException并重新测试,直到抛出“正常”异常并找出合同失败了。

Whether you then have runtime contract checking on for this assembly is a question. You have three options:

然后,您是否对此程序集进行运行时协定检查是一个问题。你有三个选择:

  1. 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. You'll then have some difficulty debugging any problems that may occur for any cases you've missed out.

    设计你的代码以确保Contract.Requires(我假设它)都不会在运行时失败(理论上它应该是这样)而不用担心它。然后,在调试任何您错过的案例时可能遇到的问题时,您会遇到一些困难。

  2. 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.

    关闭运行时检查。这样你就会得到一个“正常”的例外(虽然理论上首先不应该有)。有些人喜欢这样做,有些则不喜欢。我宁愿保留它们。

  3. Isolate any code that can't handle ContractExceptions into its own assembly and turn off runtime contract checking for this. Not necessarily ideal, but it would allow you to keep runtime checking for the rest of the code.

    将任何无法处理ContractExceptions的代码隔离到其自己的程序集中,并为此关闭运行时协定检查。不一定理想,但它允许您保持运行时检查其余代码。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2013/11/22/b96d0a7e314b5d7ce1c60c1a50c7690e.html



 
© 2014-2018 ITdaan.com 粤ICP备14056181号