删除或实体框架迁移重新创建数据库(code-一) [英] Drop or Recreate database with Entity Framework Migrations (Code-First)

查看:240
本文介绍了删除或实体框架迁移重新创建数据库(code-一)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是命令的重新的数据库时,使用实体框架迁移,不可以 初始值

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

我应该写在包管理器控制台

注释

我在找一些命令,让我相同的功能 Database.SetInitializer<>(新DropCreateDatabaseIfModelChanges<>()); 但与迁移的办法。

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

推荐答案

您可以通过使用自动迁移获得与迁移相同的行为。

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

PM> enable-migrations -EnableAutomaticMigrations

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

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

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

现在,每当你的模式的转变,只是执行更新数据库...

Now whenever your model changes, just execute 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.

这篇关于删除或实体框架迁移重新创建数据库(code-一)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆