使用EF Core DB First Design时向模型添加修改 [英] Add modifications to your model when using EF Core DB First Design
问题描述
我的团队使用Db优先设计。
我们创建数据库,然后使用Scaffold-DbContext命令创建模型。
My team uses Db first design. We create the database, then create the model using the Scaffold-DbContext command.
问题是当我们需要修改模型然后进行重新创建时
The problem is when we need to modify the model and then do a recreation.
public partial class UserInfo
{
public int Id { get; set; }
[Required]
public string FirstName { get; set; }
public string LastName { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
public DateTime RecordCreated { get; set; }
}
使用-Force调用Scaffold-DbContext后,它将删除[
Upon invoking a Scaffold-DbContext with the -Force it will remove the [Required] from it.
我应该使用ViewModel创建局部类还是什么?
Should I be looking at using a ViewModel, creating partial classes or what?
在使用EF core 2.1的早期,因此任何帮助将不胜感激。
Very early on in using EF core 2.1 so any help would be greatly appreciated.
Joe
推荐答案
如果首先使用数据库,则将数据库列设置为必需(NOT NULL),然后再次运行脚手架,而不是相反。在脚手架上,您可以选择通过流利的配置生成属性,如果这样做,则会添加 Required属性(用于引用类型)。
If you are using database first, you make the database column required (NOT NULL), and then run scaffolding again, not the other way round. When scaffolding, you can choose to generated Attributes over fluent configuration, if you do that, you will get the "Required" attribute added (for reference types).
该开关用于Scaffold-Dbontext的是-DataAnnotations
The switch for Scaffold-Dbontext is -DataAnnotations
https://docs.microsoft.com/zh-CN/ef/core/miscellaneous/cli/powershell#scaffold-dbcontext
这篇关于使用EF Core DB First Design时向模型添加修改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!