EF 核心迁移
EF Core Migrations
我正在尝试对现有数据库使用迁移并创建一个新数据库(如果不存在)。
EF Core 文档说:
If you created the initial migration when the database already exists, the database creation code is generated but it doesn't have to run because the database already matches the data model. When you deploy the app to another environment where the database doesn't exist yet, this code will run to create your database
我做了初始迁移,它创建了一个向上和向下的方法来创建我的 tables。当我 运行 它到一个新数据库(连接字符串中的新数据库名称)时,它会按预期创建数据库和 tables。
但是如果我 运行 它到一个现有的数据库(不是由迁移创建的),它在尝试创建一个已经存在的 table 的第一个 up 方法失败,并且迁移停止至 运行.
文档说 "it doesn't have to run",但迁移做的第一件事是尝试创建一个现有的 table。
如何防止迁移尝试创建现有的 table?迁移中内置了类似 "create if not exists" 的东西?文档是否正确?它应该在文档中用作 expected/describe?
如果我做错了,迁移到 运行 现有数据库和新数据库的策略是什么?
Dotnet 核心版本:1.1。
EFCore 版本:1.1.2.
提前致谢。
您需要对现有数据库进行基线迁移。在 EF 6 中,您使用了 -IgnoreChanges
标志,但现在在 Core 中不存在。参见 here。
您可以在 EF Core 中完成同样的事情,方法是注释掉所有 Up() 代码并应用该迁移。这将创建 __MigrationHistory table 并插入一条记录,表示它已被应用。
后续迁移将应用于两者,如果您需要创建一个新的数据库,您将被覆盖。
我正在尝试对现有数据库使用迁移并创建一个新数据库(如果不存在)。
EF Core 文档说:
If you created the initial migration when the database already exists, the database creation code is generated but it doesn't have to run because the database already matches the data model. When you deploy the app to another environment where the database doesn't exist yet, this code will run to create your database
我做了初始迁移,它创建了一个向上和向下的方法来创建我的 tables。当我 运行 它到一个新数据库(连接字符串中的新数据库名称)时,它会按预期创建数据库和 tables。
但是如果我 运行 它到一个现有的数据库(不是由迁移创建的),它在尝试创建一个已经存在的 table 的第一个 up 方法失败,并且迁移停止至 运行.
文档说 "it doesn't have to run",但迁移做的第一件事是尝试创建一个现有的 table。
如何防止迁移尝试创建现有的 table?迁移中内置了类似 "create if not exists" 的东西?文档是否正确?它应该在文档中用作 expected/describe?
如果我做错了,迁移到 运行 现有数据库和新数据库的策略是什么?
Dotnet 核心版本:1.1。 EFCore 版本:1.1.2.
提前致谢。
您需要对现有数据库进行基线迁移。在 EF 6 中,您使用了 -IgnoreChanges
标志,但现在在 Core 中不存在。参见 here。
您可以在 EF Core 中完成同样的事情,方法是注释掉所有 Up() 代码并应用该迁移。这将创建 __MigrationHistory table 并插入一条记录,表示它已被应用。
后续迁移将应用于两者,如果您需要创建一个新的数据库,您将被覆盖。