繁体   English   中英

EF Core 不会将可空字段的值设置为 NULL

[英]EF Core will not set value of nullable field to NULL

我有一个实体,它有一个可以为空的 int,它是一个一对一的外键。 简化,这是实体:

public class Step
{
    public int StepId { get; set; }
    public string Foo { get; set; }
    public int? RelatedStepId { get; set; }
    [ForeignKey("RelatedStepId")]
    public Step RelatedStep { get; set; }
}

这很好用——我可以将RelatedStepId设置为另一条记录的StepId的值。 但是,有时我需要打破这种关系。 如果我将RelatedStepId设置为null然后保存更改,该字段将保留其先前的值。 I have verified that the RelatedStepId truly is null , but when I log the SQL being generated (using.EnableSensitiveDataLogging() so I can see the values) I can see that it is passing the previous value in the SQL, not null .

我觉得我一定错过了一些简单的东西,但我一直在搜索和搜索,到目前为止我还没有找到任何与这个特定问题相关的东西。 如何让 EF Core 实际发送null作为RelatedStepId的值?

您也应该将RelatedStep设置为 null。 我猜你没有清除这个 object。

填充RelatedStepIdRelatedStep之一足以让EF保存该值,如果您不想要它,则必须清除它们。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM