我将现有属性decimal
设置为decimal?
:
public decimal? TotalAmountTTC { get; set; }
然后我用add-migration
创建了一个迁移,它为我生成了这个:
migrationBuilder.AlterColumn<decimal>(
name: "c0003_total_amount_ttc",
table: "t0003_transfer_request",
type: "decimal(13,4)",
nullable: true,
oldClrType: typeof(decimal),
oldType: "decimal(13,4)");
但是我执行update-database
后,该列仍然是not nullable
:
当我运行script-migration
来检查生成的SQL时,我们可以清楚地看到它并不在乎我的属性现在可以为空:
ALTER TABLE "t0003_transfer_request" MODIFY "c0003_total_amount_ttc" decimal(13,4)
/
我做错什么了吗?这是一个错误吗?
我尝试在映射中设置IsRequired(false)
,但结果相同。
builder.Property(tr => tr.TotalAmountTTC).HasColumnName("c0003_total_amount_ttc").IsRequired(false);
好吧,对我来说好像是一个[[hack,但是我找到了一种方法来将NULLABLE
设置为已经存在的NOT NULL
列:
NULL
(在我的情况下为decimal(13,4) NULL
): public void Configure(EntityTypeBuilder<TransferRequest> builder)
{
builder.Property(tr => tr.TotalAmountTTC).HasColumnType("decimal(13,4) NULL");
}