Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework code first: update SetInitializer throws exception

I've been looking for this strange error for hours but haven't found anything. I have a very simple entity:

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 running the first time, everything works just fine. But, when I change the entity (for example, I put the [Key] attribute for Id), I get the expected "model has changed" or something error. So, I enter this in the Global.asax application_start:

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

This is where I get stuck. There is no compile error, it compiles 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. Please review the stack trace for more information about the error and where it originated 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, does someone have a clue? I'm using Entity Framework 4.1 (CTP5)

like image 296
user825887 Avatar asked May 10 '11 21:05

user825887


1 Answers

For the unanswered question trawlers, this has been answered above. But never marked by users. See comments above. Over 1K views ! Thats wasting a lot of time... So i added this.

like image 184
phil soady Avatar answered Sep 22 '22 07:09

phil soady