如何禁用代码首次迁移

我在 EF5中有一个代码优先实体模型。但是我希望手动管理数据库更改——我不希望 EF 修改我现有的数据库及其所有数据。但是当我在 EF 映射和数据库中进行并行更改时,EF 拒绝正确操作,告诉我需要使用代码优先迁移。我要怎么关掉这个?

103804 次浏览

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.

set the Database.SetInitializer to 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);
}

So the most complete answer that I have found is this:

  1. Delete Migrations folder inside your project.
  2. Set Database.SetInitializer<DatabaseContext>(null); inside your DatabaseContext initializer.
  3. Delete the table __MigrationHistory inside your database. For EF6+ the table is located under Tables but for earlier versions it is located under System Tables.
  4. Build and run.
  5. Profit.

I just resolved this "issue" by

  1. Deleting table "_MigrationHistory" from the database.
  2. Deleting "Migrations" folder form the project.
  3. Updating EDMX file.
  4. Clean project & rebuild it.

The config of my environment is following

1. Visual Studio 2017 15.8.2
2. ASP NET MVC project
3. .NET Framework 4.6.1
4. Entity Framework 6.2.0