使用Entity Framework迁移删除或重新创建数据库(代码优先)

时间:2022-01-10 09:50:51

Which is the command to recreate or drop the database when using Entity Framework Migrations, not Initializers?

在使用Entity Framework迁移时,哪个是重新创建或删除数据库的命令,而不是初始化程序?

What should I write on the package manager console?

我应该在包管理器控制台上写什么?

COMMENT:

评论:

I'm looking for some command that gives me the same functionality as Database.SetInitializer<>(new DropCreateDatabaseIfModelChanges<>()); but with the migrations approach.

我正在寻找一些命令,它给我提供与Database.SetInitializer <>相同的功能(new DropCreateDatabaseIfModelChanges <>());但随着迁移的方法。

3 个解决方案

#1


7  

You can get the same behavior with Migrations by using automatic migrations.

您可以使用自动迁移在迁移中获得相同的行为。

PM> enable-migrations -EnableAutomaticMigrations

In Configuration.cs in the constructor, make sure automatic migrations and allow data loss are set to true...

在构造函数的Configuration.cs中,确保自动迁移和允许数据丢失设置为true ...

public Configuration()
{
    AutomaticMigrationsEnabled = true;
    AutomaticMigrationDataLossAllowed = true;
}

Now whenever your model changes, just execute update-database...

现在,只要模型发生变化,只需执行update-database ...

PM> update-database

The schema will be changed to match the models and any existing data in the tables will stay there (unless it's in a renamed or removed column). The Seed method is run after the database is updated so you can further control any changes to the data in there.

将更改架构以匹配模型,并且表中的任何现有数据将保留在那里(除非它位于重命名或删除的列中)。在更新数据库之后运行Seed方法,以便您可以进一步控制对数据的任何更改。

#2


1  

I went into server explorer and manually deleted all the tables. Then went to the project\Migrations folder and deleted all the migration scripts.

我进入服务器资源管理器并手动删除所有表。然后转到project \ Migrations文件夹并删除所有迁移脚本。

Then a plain old Update-Database -Force did what I needed.

然后一个普通的旧的Update-Database -Force做了我需要的。

#3


0  

In VS2015, I did the following to resolve it: In solution explorer menu-->Show All-->The App_Data folder shows the LocalDb mdf file-->Right click the file and click Delete-->In Package Manage Condsole, run: Update-Database -Force

在VS2015中,我执行了以下操作来解决它:在解决方案资源管理器菜单 - >全部显示 - > App_Data文件夹显示LocalDb mdf文件 - >右键单击该文件,然后单击删除 - >在包管理控制台中,运行:Update-Database -Force

#1


7  

You can get the same behavior with Migrations by using automatic migrations.

您可以使用自动迁移在迁移中获得相同的行为。

PM> enable-migrations -EnableAutomaticMigrations

In Configuration.cs in the constructor, make sure automatic migrations and allow data loss are set to true...

在构造函数的Configuration.cs中,确保自动迁移和允许数据丢失设置为true ...

public Configuration()
{
    AutomaticMigrationsEnabled = true;
    AutomaticMigrationDataLossAllowed = true;
}

Now whenever your model changes, just execute update-database...

现在,只要模型发生变化,只需执行update-database ...

PM> update-database

The schema will be changed to match the models and any existing data in the tables will stay there (unless it's in a renamed or removed column). The Seed method is run after the database is updated so you can further control any changes to the data in there.

将更改架构以匹配模型,并且表中的任何现有数据将保留在那里(除非它位于重命名或删除的列中)。在更新数据库之后运行Seed方法,以便您可以进一步控制对数据的任何更改。

#2


1  

I went into server explorer and manually deleted all the tables. Then went to the project\Migrations folder and deleted all the migration scripts.

我进入服务器资源管理器并手动删除所有表。然后转到project \ Migrations文件夹并删除所有迁移脚本。

Then a plain old Update-Database -Force did what I needed.

然后一个普通的旧的Update-Database -Force做了我需要的。

#3


0  

In VS2015, I did the following to resolve it: In solution explorer menu-->Show All-->The App_Data folder shows the LocalDb mdf file-->Right click the file and click Delete-->In Package Manage Condsole, run: Update-Database -Force

在VS2015中,我执行了以下操作来解决它:在解决方案资源管理器菜单 - >全部显示 - > App_Data文件夹显示LocalDb mdf文件 - >右键单击该文件,然后单击删除 - >在包管理控制台中,运行:Update-Database -Force