EF代码优先,现有数据库位于另一个数据库上 [英] EF Code First with existing database on another database

查看:137
本文介绍了EF代码优先,现有数据库位于另一个数据库上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标题有点令人困惑,但是我试图指定问题不是针对现有数据库启动代码优先模型和迁移,而是在此之后.

The title is a bit confusing but I'm trying to specify that the problem is not initiating a code first model and migration for an existing database, but comes after that.

我需要激活自动迁移,因为我们已切换到系统的代码优先模型.所以,这是已经完成的事情:

I needed to activate automatic migration because we switched to a code first model for our system. So, here's what has been done:

  1. 我为现有数据库创建了一个空的InitialCreate
  2. 我做了一些其他脚本,因为进行了一些更改,这些脚本工作正常,并且脚本已创建并在数据库上运行

当我想使用那些脚本并迁移尚未通过这种方式初始化的另一个数据库时,就会发生问题.我不知道该怎么办.

The problem happen when I want to use those script and migrate another database that was not yet initialized this way. I don't know what to do.

当我尝试运行更新数据库时,出现错误:

When I try to run Update-database I get the error:

Unable to update database to match the current model because there are pending changes and automatic migration is disabled. Either write the pending model changes to a code-based migration or enable automatic migration. Set DbMigrationsConfiguration.AutomaticMigrationsEnabled to true to enable automatic migration.
You can use the Add-Migration command to write the pending model changes to a code-based migration.

如果我执行Add-Migration,它将再次创建包含所有创建表的所有迁移,就像它忽略了我当前的脚本一样,一个是InitialCreate空,另一个是脚本.

If I do a Add-Migration it creates a migration with everything in it again, all the create tables, it's like it ignores my currents scripts, the fact that there is a InitialCreate empty and the other scripts.

推荐答案

好的,您有2个数据库-假设DEV和PROD.在启用迁移之前,两者都处于相同的状态并具有相同的架构.这是要做什么:

OK, you have 2 databases - let's say DEV and PROD. Both are in an identical state and have the same schema before migrations have been enabled. This is what to do:

1-将迁移添加到DEV环境,并将数据库初始化程序设置为 MigrateDatabaseToLatestVersion .另一个选择是以编程方式运行迁移.

1 - Add migrations to your DEV environment and set your database initializer to MigrateDatabaseToLatestVersion. Another option is to programatically run migrations.

enable-migrations
// take a snapshot of current state. -IgnoreChanges prevents recreate of existing objects.
add-migration InitialBaseline -IgnoreChanges
update-database

2-有几种方法可以使其他数据库保持同步:

2 - There are several ways to keep the other database(s) in sync:

A)通过更改连接字符串来并行维护迁移.因此,指向PROD,然后运行update-database创建__MigrationHistory表并应用初始的空白基线.我不建议对PROD数据库使用此选项(请参见下文).

A) Maintain migrations in parallel by changing the connection string. So point at PROD, and run update-database to create the __MigrationHistory table and apply the initial, blank, baseline. I don't recommend this option for PROD databases (see below).

B)与脚本同步.许多组织不希望EF应用更改,而是要求DBA应用脚本.对于此选项,您可能需要将数据库初始化程序设置为NULL.在这种情况下,您可以执行update-database -Script来生成更改.这将在初始基准之后的迁移上完成,因为它们已经同步.有关此技术的更多信息,请参见此处.

B) Sync with scripts. Many organizations don't want EF applying changes and instead require DBAs to apply scripts. For this option, you may want to set your database initializer to NULL. In this case you can do an update-database -Script to generate changes. This would be done on migrations subsequent to your initial baseline since they are already in sync. See here for more info on this technique.

C)使用数据库项目或diff工具使事物保持同步.

C) Use database projects or a diff tool to keep things in sync.

现在,当您在DEV中更改模型时

Now when you go and change your models in DEV:

add-migration Changes1
update-database

对于选项A,更改连接字符串并重复.对于选项B,使用update-database -Script.对于选项C,请与工具重新同步.

For option A, change connect string and repeat. For option B, use update-database -Script. For option C, resync with tool.

注意:我需要激活自动迁移..."-自动迁移是完全不同的事情,可能会使过程复杂化.请参见此处.

NOTE: "I needed to activate automatic migrations..." - Automatic migrations are a whole different matter and can complicate the process. See here.

这篇关于EF代码优先,现有数据库位于另一个数据库上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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