How can I disable code first migrations

I have a code-first entity model in EF5. But I want to manage the database changes manually -- I do not want EF to modify my existing database and all its data. But when I make parallel changes in the EF mapping and in the database, EF refuses to operate properly telling me I need to use code first migration. How do I turn this off?


将Database.SetInitializer设置为null。

public class DatabaseContext: DbContext
{
    //the base accepts the name of the connection string provided in the web.config as a parameter
    public DatabaseContext()
        : base("DatabaseContext")
    {
        //disable initializer
        Database.SetInitializer<DatabaseContext>(null);
    }

If you want to completely turn off migrations:

https://stackoverflow.com/a/9709407/141172

However, I found it better to keep code first migrations enabled, but use the -Script option to have EF create a DB change script for me that I can apply to each database (development, QA, Production) manually:

Update-Database -Script -ProjectName MyProject -StartupProjectName MyProject

That way EF will create the change script for me, and I still have full control over changes being applied. I version the change scripts like any other source code.


If you already used Migrations then changing only Initializer won't help. You need to go to Management Studio, open your database tables, go to System Tables folder and remove __MigrationHistory table that is located there (for EF6 and above, it's located directly under Tables ). This will disable Migrations for good.

链接地址: http://www.djcxy.com/p/90240.html

上一篇: C#实体框架迁移

下一篇: 我如何禁用代码第一次迁移