gpt4 book ai didi

c# - Entity Framework 代码首先使列不可为空

转载 作者:可可西里 更新时间:2023-11-01 07:46:56 25 4
gpt4 key购买 nike

我的项目首先使用 EF 代码。我的数据模型中有以下代码

[HiddenInput(DisplayValue = false)]        
public DateTime? PasswordDate { get; set; }

为了使其不可为空,我删除了“?”并从包管理器控制台运行 Add-Migration 命令。生成了以下迁移文件。

  public partial class PasswordDate : DbMigration
{
public override void Up()
{
AlterColumn("dbo.CertificateInfoes", "PasswordDate", c => c.DateTime(nullable: false));
}

public override void Down()
{
AlterColumn("dbo.CertificateInfoes", "PasswordDate", c => c.DateTime());
}
}

但是当我运行 Update-Database 命令时:

Update-Database -SourceMigration 201309020721215_PasswordDate

我收到以下错误:无法将值 NULL 插入表“PasswordDate”列;列不允许空值。更新失败。声明已终止。

请提出解决方案。

最佳答案

那是因为您允许在该列中使用 NULL 值,然后尝试使其不可为 null。随后它将尝试将您现有的数据迁移到新的不可空列中,这将中断,因为您已经在其中包含了 NULL 值。

两种解决方案:

1) 改回可空
2) 为没有值的项目赋予默认值。

关于c# - Entity Framework 代码首先使列不可为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18569003/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com