无法将 NULL 值插入列“描述”中。 EF 核心 5
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通过将 string 更改为 string 解决的问题?:
更新到 Dot NET core 5 之前,该问题不存在。这有点令人困惑,因为 string 是引用类型,默认情况下可为空。
编辑:C# 8 引入了一项名为可为空引用类型 (NRT) 的新功能,默认情况下它的值为“注释”(除非使用 ? 声明,否则不可为空)。要禁用此功能,请打开 csproj 文件并编辑它:
详细信息
The problem solved by changing string to string?:
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:
Details