First enter Entity Framework code: update SetInitializer throws an exception

I searched for this strange error for hours, but found nothing. I have a very simple object:

public class Company { public Guid Id { get; set; } public string Name { get; set; } } 

And here is the context:

 public class MyDBContext : DbContext { public DbSet<Company> Companies { get; set; } } 

When starting for the first time, everything works fine. But, when I change the entity (for example, I put the [Key] attribute for Id), I get the expected โ€œmodel changedโ€ or something confusing. So, I introduced this into the Global.asax application_start application:

 Database.SetInitializer<MyDBContext> (new DropCreateDatabaseIfModelChanges< MyDBContext >()); 

This is where I am stuck. A compilation error does not compile without errors / warnings. But when I run my project, I get the following error:

Description: An unhandled exception occurred during the execution of the current web request. Check the stack trace for more information about the error and where it appeared in the code.

Exception Details: System.TypeLoadException: GenericArguments[0], 'MyDB.Data.MyDBContext', on 'System.Data.Entity.IDatabaseInitializer1[TContext]' violates the constraint of type parameter 'TContext' .

Please someone have a key? I am using Entity Framework 4.1 (CTP5)

+7
source share
2 answers

For the unanswered questions of the trawlers, this was answered above. But never tagged by users. See comments above. Over 1 thousand views! Thats wasting a lot of time ... so I added this.

+2
source

If you work with Web.config (instead of Global.asax) and several project files (for example, I usually create an [Projectname] .WUUI file in ASP.NET MVC that contains MVC files and the name [Projectname]. The domain that represents class library containing data logic), then you need to add the following code for ALL Web.config / App.config to your solution:

 <configSections> <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> </configSections> 

This worked for me without having to combine the domain project with the WebUI project.

0
source

All Articles