EntityFramework核心生产迁移 [英] EntityFramework Core production migrations

查看:72
本文介绍了EntityFramework核心生产迁移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何更新数据库以进行生产?我已将连接字符串秘密移出应用程序。我们的CI / CD管道处理保留在应用程序设置中的连接字符串模板内的令牌替换,并且开发人员将无权访问生产数据库。

How do I update the database for production? I've moved the connection string secrets out of the application. Our CI/CD pipeline handles the token replacement within the connection string "template" that remains in the appsettings, and developers won't have production database access.

使用自动迁移,直到今天才发现Microsoft取消了EF Core。

I had expected to use automatic migrations, only to discover today that Microsoft eliminated that for EF Core.

我每次都必须临时覆盖通常指定我本地的连接字符串数据库的dev副本?似乎是这样,但对我来说似乎也很麻烦,所以我想知道是否丢失了某些东西。

Will I have to, every time, temporarily overwrite the connection string that normally specifies my local dev copy of the database? It seems that way, but it also seems very janky process to me, so I wonder if I'm missing something.

推荐答案

迁移由两部分组成:


  1. 从模型生成的

  2. 应用于数据库。

迁移生成又可以是两种类型:

Migration generation in turn could be two types:


  • 自动-在运行时,将当前模型与由最后一次迁移,如果有更改,它会生成一个自动迁移代码。

  • 手动-迁移是在设计时使用设计时工具生成的(添加迁移命令)。自动生成的代码可以修改。

  • automatic - at runtime, current model is compared to model produced by the last migration, and if there are changes, it generates an automatic migration code.
  • manual - migrations are generated at design time using the design time tools (Add-Migration command). The auto-generated code can be modified. Modified or not, the migration is stored along with the application code.

不管迁移是如何产生的,它们都可以在设计时使用工具( Update-Database 命令)和/或在运行时应用。

Regardless of how migrations are generated, they can be applied at design time using the tools (Update-Database command) and/or at runtime.

EF6支持和EF Core删除的是自动生成。 EF Core迁移无法在运行时生成-它们必须在设计时生成,并与应用程序代码一起存储。

What EF6 supports and EF Core have removed is the auto-generation. EF Core migrations cannot be generated at runtime - they must be generated at design time and stored with the application code.

在运行时应用它们是通过在某些应用程序启动点调用 Migrate 方法实现的。

Applying them at runtime is achieved with Migrate method called from some application startup point:

dbContext.Database.Migrate();

有关更多信息,请参见迁移在运行时应用迁移 EF Core文档主题。

For more info, see Migrations and Apply migrations at runtime EF Core documentation topics.

这篇关于EntityFramework核心生产迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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