无法将 NULL 值插入列“描述”中。 EF 核心 5

发布于 2025-01-13 22:49:47 字数 1501 浏览 0 评论 0原文

我正在使用 Entity Framework Core,并且有这个模型类:

public class ConstraintRule
{
    public int Id { get; set; }
    public bool Enabled { get; set; }
    public PlantFilterType PlantFilterType { get; set; }
    public DateTime? FromDate { get; set; }
    public DateTime? ToDate { get; set; }
    public string PowerplantIds { get; set; } 
    public string PowerplantTypes { get; set; } 
    public User User { get; set; }
    public DateTime CreationTime { get; set; }
    public string Description { get; set; }
    public int Priority { get; set; }
}

这是与 ConstraintRule 模型相关的上下文中的代码:

 builder.Entity<ConstraintRule>(entity =>
        {
            entity.Property(e => e.Description).HasMaxLength(500);
            entity.Property(e => e.PowerplantIds).HasMaxLength(2000);
            entity.Property(e => e.PowerplantTypes).HasMaxLength(2000);
        });

描述是一个可为空的字符串,这是迁移生成的代码(也是 PowerplantIds 和 < code>PowerplantTypes 有同样的问题):

 migrationBuilder.AlterColumn<string>(
            name: "Description",
            table: "ConstraintRules",
            type: "nvarchar(500)",
            maxLength: 500,
            nullable: false,
            defaultValue: "",
            oldClrType: typeof(string),
            oldType: "nvarchar(500)",
            oldMaxLength: 500,
            oldNullable: true);

我收到此错误:

无法将 NULL 值插入“描述”列

I am using Entity Framework Core and I have this model class:

public class ConstraintRule
{
    public int Id { get; set; }
    public bool Enabled { get; set; }
    public PlantFilterType PlantFilterType { get; set; }
    public DateTime? FromDate { get; set; }
    public DateTime? ToDate { get; set; }
    public string PowerplantIds { get; set; } 
    public string PowerplantTypes { get; set; } 
    public User User { get; set; }
    public DateTime CreationTime { get; set; }
    public string Description { get; set; }
    public int Priority { get; set; }
}

This is the code in the context related to ConstraintRule model:

 builder.Entity<ConstraintRule>(entity =>
        {
            entity.Property(e => e.Description).HasMaxLength(500);
            entity.Property(e => e.PowerplantIds).HasMaxLength(2000);
            entity.Property(e => e.PowerplantTypes).HasMaxLength(2000);
        });

Description is a nullable string, this is the code generated by migration (also PowerplantIds and PowerplantTypes have the same problem):

 migrationBuilder.AlterColumn<string>(
            name: "Description",
            table: "ConstraintRules",
            type: "nvarchar(500)",
            maxLength: 500,
            nullable: false,
            defaultValue: "",
            oldClrType: typeof(string),
            oldType: "nvarchar(500)",
            oldMaxLength: 500,
            oldNullable: true);

I get this error:

Cannot insert the value NULL into column 'Description'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

短叹 2025-01-20 22:49:48

通过将 string 更改为 string 解决的问题?:

public string? Description { get; set; }

更新到 Dot NET core 5 之前,该问题不存在。这有点令人困惑,因为 string 是引用类型,默认情况下可为空。

编辑:C# 8 引入了一项名为可为空引用类型 (NRT) 的新功能,默认情况下它的值为“注释”(除非使用 ? 声明,否则不可为空)。要禁用此功能,请打开 csproj 文件并编辑它:

 <PropertyGroup>  
 <Nullable>disable</Nullable>

详细信息

The problem solved by changing string to string?:

public string? Description { get; set; }

The problem doesn't exist before updating to Dot NET core 5. This is a little confusing because string is a reference type and by default is nullable.

Edit: C# 8 introduced a new feature called nullable reference types (NRT), it's value is "annotations" by default (Non-nullable unless declared with ?). To disable this feature open csproj file and edit it:

 <PropertyGroup>  
 <Nullable>disable</Nullable>

Details

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文