简体   繁体   English

使用Entity Framework,Code First(POCO)和Code Contracts运行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 . 我有一个项目使用Entity Framework 6POCO 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: 你有三个选择:

  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. 设计你的代码以确保Contract.Requires (我假设它)都不会在运行时失败(理论上它应该是这样)并且不用担心它。 You'll then have some difficulty debugging any problems that may occur for any cases you've missed out. 然后,在调试任何您错过的案例时可能会遇到的任何问题都会遇到一些困难。

  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. 将任何无法处理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.

相关问题 实体框架代码第一次更新 - 数据库在CREATE DATABASE上失败 - Entity Framework code first update-database fails on CREATE DATABASE 实体框架代码优先迁移因更新数据库而失败 - Entity Framework code-first migration fails with update-database 实体框架代码优先:使用&#39;Update-Database&#39;生成SQL脚本在解析时会产生XML错误 - Entity Framework Code-First: Generate SQL script with 'Update-Database' produces XML error while parsing 更新数据库上的ASP.NET代码优先(实体框架)错误 - ASP.NET Code-First (Entity Framework) Error on update-database 运行代码第一次迁移update-database时出错 - Error running code first migration update-database 尝试首先更新数据库代码时出错 - Error on trying to Update-Database code first 使用代码优先工作流使用 Update-Database 时出现异常 - Exception when using Update-Database using code first workflow 实体框架代码优先 - 如何为生产数据库运行 Update-Database - Entity framework code first - how to run Update-Database for production database 实体框架代码优先:类型类型的计算属性导致Update-Database上的ArgumentNullException - Entity Framework Code First: Computed property of type Type causes ArgumentNullException on Update-Database 实体框架代码优先:迁移失败,更新数据库,强制不必要的(?)添加迁移 - Entity Framework code-first: migration fails with update-database, forces unneccessary(?) add-migration
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM