.NET Core 数据库模型在生产中的变化 [英] .NET Core database model changes in production

查看:31
本文介绍了.NET Core 数据库模型在生产中的变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遵循代码优先的方法通过 EFCore 生成我的数据库.现在在开发过程中,我的一个模型发生了变化.如何在我们的一位客户的实时系统上更新相应的数据库表?我当然不想转储或删除任何数据.

I follow code first approach to generate my databases by EFCore. Now during development one of my models changed. How can I update the corresponding database tables on a live system at one of our customers? I do not want to dump or delete any data of course.

我仍在使用 .NET Core 1.1.0.

I'm still on .NET Core 1.1.0.

推荐答案

我担心到时候你不得不吞下药丸.

I fear you'll have to swallow the pill then.

Database.EnsureCreated() 仅对开发有用,您无需保留数据.如果您必须保留数据或更改表架构,则必须使用迁移或数据库脚手架(从数据库架构创建模型).

Database.EnsureCreated() is only useful for developing, where you don't have to preserve data. If you have to preserve data or change the table schema, you have to use migrations or database scaffolding (create models from Database schema).

无论如何,在应用/尝试以下步骤之前备份生产数据,并在临时服务器上预先尝试.

In any case, do a backup of the production data, before applying/attempting following steps and try it out upfront on a staging server.

一个敏感的选择是......

One touchy option would be...

  1. 恢复您的代码(即返回到 VCS 中的旧版本)
  2. 基于它创建一个表格布局(运行 Add-Migration InitialVersiondotnet ef migrations add InitialVersion 然后是 Update-Database 或 <代码>dotnet ef 数据库更新
  3. 重新应用您的模型更改(返回到 VCS 中的当前版本,但将文件保留在 Migration 文件夹中)
  4. 运行 Add-Migration ModelUpdateV2dotnet ef migrations add ModelUpdateV2
  5. 运行Script-Migration(感谢@Smit!)或dotnet ef 迁移脚本.这将生成一个 SQL 命令,用于将更改应用于架构
  1. restore your code (i.e. going back to an older revision in your VCS)
  2. create a table layout based on it (running Add-Migration InitialVersion or dotnet ef migrations add InitialVersion followed by Update-Database or dotnet ef database update
  3. reapply your model changes (going back to your current revision in VCS, but keeping the files in the Migration folder)
  4. run Add-Migration ModelUpdateV2 or dotnet ef migrations add ModelUpdateV2
  5. run Script-Migration (Thanks @Smit!) or dotnet ef migrations script. This will generate an SQL command for applying the changes to the schema

从现在开始你有两个选择:

From now on you have two options:

  1. 在您的数据库开发数据库中查找__EFMigrationHistory表.它应该包含一个带有您的初始迁移名称的条目.将此表导出到您的生产系统中.然后,您的应用程序应该在下次启动时应用迁移(当您在 Startup.cs 中调用 context.Database.MigrateAsync() 时).

  1. Look for a __EFMigrationHistory table within your database on the development database. It should contain one entry with your initial migration name. Export this table into your production system. Then you application should apply the migrations on the next start (when you call context.Database.MigrateAsync() within your Startup.cs).

从现在开始你应该可以在未来使用迁移

From now on you should be able to use migrations in future

只需从上面执行迁移脚本,无需复制__EFMigrationHistory,但是您必须重复上述步骤.

Just execute the migration script from above, without copying over __EFMigrationHistory, but then you'll have to repeat the steps above.

另一个选项是,始终从数据库创建模型(请参阅 dotnet ef dbcontext scaffold 命令(请参阅 EF Core Docs) 将来从数据库架构创建模型.

The other option is, to always create the models from the database (see dotnet ef dbcontext scaffold command (see EF Core Docs) do create models from database schema in future.

然后每次更改表时,您都必须创建自己的 SQL 以进行迁移,并在部署应用程序之前或部署应用程序时应用该 SQL.

Then every time you change the table, you have to create your own SQL for migration and apply that before or while deploying the application.

这篇关于.NET Core 数据库模型在生产中的变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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