【发布时间】:2021-12-17 16:21:24
【问题描述】:
迁移认为电子邮件现在应该可以为空,我看不出它是从哪里提出这个想法的。迁移前的数据库不为空。运行Add-Migration 会产生下面的代码更改,其中电子邮件现在标记为可为空;但是,字符串上存在 [Required] 属性,并且此代码已经有一段时间没有改变了。还有哪些其他可能导致这种行为的副作用? (目标是 SQL 服务器)
(1) 模型不变:
[Table("UserProfile")]
public class UserProfile
{
// .. snip...
[JsonProperty("email"), Required, Display(Name = "Email")]
public string Email { get; set; }
}
(2)
Add-Migration ExampleMigration
(3) 输出迁移:
public partial class ExampleMigration : DbMigration
{
public override void Up()
{
AlterColumn("dbo.UserProfile", "Email", c => c.String());
}
public override void Down()
{
AlterColumn("dbo.UserProfile", "Email", c => c.String(nullable: false));
}
}
比较 resx 迁移架构显示相同的结果,其中最新迁移删除了 Nullable="false"。
之前
<Property Name="Email" Type="String" MaxLength="Max" FixedLength="false" Unicode="true" Nullable="false" />
之后
<Property Name="Email" Type="String" MaxLength="Max" FixedLength="false" Unicode="true" />
【问题讨论】:
-
您的项目是否引用了任何 .NET Standard 库,或者引用了
System.ComponentModel.DataAnnotationsNuGet 包?如果是这样,它可能与this issue有关。
标签: entity-framework