7

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)

6
  • 3
    F*ck this, solved after hours. Turned out the library class was using 4.0.0 and the web application 4.0.1. Shoot me. Commented May 10, 2011 at 21:32
  • 3
    Sir, you just saved me guaranteed hours of banging my head against a desk. Put your answer up and I'll upvote it immediately. Commented Jun 5, 2012 at 18:40
  • 1
    If this was due to a Fusion Load error... the fusion log viewer is your friend. I have also seen something similar When EF is in Auto Migration mode, it will expect a Context with parameter less constructor. Commented May 10, 2013 at 14:43
  • 1
    mark as answered please Commented May 19, 2013 at 14:40
  • 1
    possible duplicate of TypeLoadException with DbContext Commented May 28, 2013 at 8:41

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

If you are working with Web.config (instead of Global.asax) and multiple project files (I for example usually create in ASP.NET MVC a [Projectname].WebUI which contains the MVC files and a [Projectname].Domain which is a class library containing the data logic) then you need to add following code to ALL Web.config/App.config in 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 needing to merge my Domain project with the WebUI project.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.