实体框架5代码迁移 [英] Entity Framework 5 Code Migrations

查看:91
本文介绍了实体框架5代码迁移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  1. 有关当前数据库状态(应用了什么迁移)的信息存储在哪里?我想它可以是 dbo .__ MigrationHistory表还是该表仅用于记录目的?

  1. Where is stored information about current database state (what migration is applied)? I suppose it can be "dbo.__MigrationHistory" table or this table is just for logging purpose?

如果启用了迁移,则添加迁移并更新数据库。之后,我将代码检入到SVN中,另一位开发人员将其检出。

If I enabled migration, added migration and updated my database. After that I checked-in code to SVN and another developer checked it out. What this another developer has to do to create/update his own database?

我看到这样的选择:

>

1)立即调用Update-Database命令。

1) Call Update-Database command right away.

2)从头开始做所有事情(启用迁移,添加-迁移,更新数据库)。

2) Do everything from beginning (Enable-Migration, Add-Migration, Update-Database).

3)进行所有操作,但跳过添加迁移步骤(该步骤已经存在,为每个新开发人员再次添加它似乎很奇怪。 )。

3) Do everything but skip Add-Migration step (it is already present and it seems strange to add it once again for every new developer).

我的假设是正确的,或者如果没有人在正确的地方呢?

Which of my assumption is right or if no one where is the right way?

推荐答案


  1. 要检索已应用到数据库的迁移,可以使用 Get-Migrations 命令(参考)。

一切取决于创建数据库的方式以及所使用的初始化程序。
如果您不熟悉这些文章,则值得阅读。

Everything depends on how the database is created, which initializer you are using. This article is worth reading if you are unfamiliar with those.


  • 使用 DropCreateDatabaseAlways 初始值设定项,您实际上不需要关心更新数据库,因为您的数据库将被删除&

  • When using the DropCreateDatabaseAlways initializer, you don't really need to care about updating your database, because your database will be deleted & recreated at each application startup.

在使用 DropCreateDatabaseWhenModelChanges initiliazer时,将删除您的数据库,然后重新创建如果EF在应用程序启动时检测到模型已更改。

When using the DropCreateDatabaseWhenModelChanges initiliazer, your database will be dropped then recreated if EF detects the model has changed at the application startup.

使用 CreateDatabaseIfNotExists 初始化程序时,或者尚未定义初始化程序,如果数据库不存在,则将创建它。如果数据库已经存在&添加迁移后,您(以及每个检索代码的开发人员)都需要使用 Update-Database 命令来更新数据库。

When using the CreateDatabaseIfNotExists initilizer or if no initializer has been defined, your database will be created if it does not already exists. If the database already exists & you added a Migration, you (and every developer retrieving your code) need to use the Update-Database command to update the database.

代码优先迁移引入了一个新的初始化程序: MigrateDatabaseToLatestVersion ,该初始化程序将数据库自动更新为定义的最新迁移。
请参阅本页的最后部分: http://msdn.microsoft.com/en -us / data / jj591621

There is a new initializer introduced with Code-First Migrations: MigrateDatabaseToLatestVersion, this initializer automatically update the database to the latest Migration defined. See this page's last section: http://msdn.microsoft.com/en-us/data/jj591621.

这篇关于实体框架5代码迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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