Entity Framework 6.1.1 disable model compatibility

2019-02-01 22:23发布

I am running into the following error after updating EF to version 6.1.1:

An unhandled exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll

Additional information: The model backing the TvstContext context has changed since the database was created. Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?LinkId=238269).

We could fix this in the past as described in this question: Where is modelBuilder.IncludeMetadataInDatabase in EF CTP5?

However, I can't seem to get rid of the error.

3条回答
再贱就再见
2楼-- · 2019-02-01 22:54

You can also set it in Web.config file

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <appSettings>    
    <add key="DatabaseInitializerForType YourNamespace.YourDbContext, AssemblyName"
            value="Disabled" />
    </appSettings>
</configuration>

Decision from: http://www.entityframeworktutorial.net/code-first/turn-off-database-initialization-in-code-first.aspx

查看更多
Fickle 薄情
3楼-- · 2019-02-01 22:59

This should work, put it somewhere in the constructor of your derived DbContext:

Database.SetInitializer<MyContext>(null);
查看更多
聊天终结者
4楼-- · 2019-02-01 23:01

To expand on Dabblernl's answers:

public ApplicationDbContext()
        : base("DefaultConnection", throwIfV1Schema: false)
    {
        Database.SetInitializer<ApplicationDbContext>(null);
    }
查看更多
登录 后发表回答