3
votes

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.

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

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.

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

1
I've had the same problem, I'm guessing it's a problem with the ContractException class being internal and therefore not raising the exception detail to the console, possibly a problem with crossing the 'AppDomain' boundary. - nicodemus13

1 Answers

0
votes

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?

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.

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.

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.

  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.