使用数据库优先方法时如何更新模型 [英] How to update the model when using database first approach

查看:68
本文介绍了使用数据库优先方法时如何更新模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我首先使用EntityFramework Core数据库创建模型如EF Core文档中所述

I used EntityFramework Core database first to create model as illustrated in the EF Core documentation

但是我不知道在数据库被编辑后如何更新模型.

But I don't know how to update the model when the database has been edit.

推荐答案

您可以通过运行最初添加了-Force选项的命令来重新构建模型.这将导致指定文件夹的内容被覆盖.使用软件包管理器控制台示例来自EF Core文档,修改后的命令将变为:

You can re-scaffold the model by running the command that you originally ran with the -Force option added. That will result in the contents of the specified folder being over-written. Using the Package Manager Console example from the EF Core docs, the revised command becomes:

Scaffold-DbContext "Server=(localdb)\v11.0;Database=Blogging;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Force

或者,如果您使用的是 CLI命令,它将变为:

Alternatively, if you are using CLI commands, it becomes:

dotnet ef dbcontext scaffold "Server=(localdb)\v11.0;Database=Blogging;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -o Models -f

但是,您应该考虑使用迁移"来使模型和数据库架构彼此保持同步.这样,您可以对模型进行更改,然后将其传播到数据库.

However, you should consider using Migrations to keep your model and database schema in sync with each other. That way you make changes to the model and then propagate them to the database.

这篇关于使用数据库优先方法时如何更新模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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