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

查看:126
本文介绍了.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 InitialVersion dotnet ef迁移会添加InitialVersion ,然后是 Update-Database dotnet ef数据库更新

  3. 重新应用模型更改(返回到VCS中的当前版本,但将文件保留在Migration文件夹中)

  4. 运行添加迁移ModelUpdateV2 dotnet ef迁移添加ModelUpdateV2

  5. 运行脚本迁移(感谢@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核心文档)将来会从数据库架构中创建模型。

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天全站免登陆