[英]EF Core will not set value of nullable field to NULL
I have an entity that has a nullable int which is a one-to-one foreign key to itself.我有一个实体,它有一个可以为空的 int,它是一个一对一的外键。 Simplified, this is the entity:简化,这是实体:
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; }
}
This works fine - I'm able to set the RelatedStepId
to the value of another record's StepId
.这很好用——我可以将RelatedStepId
设置为另一条记录的StepId
的值。 However, there are times when I need to break this relationship.但是,有时我需要打破这种关系。 If I set RelatedStepId
to null
and then save changes, the field retains its previous value.如果我将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
. 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
.
I feel like I must be missing something simple, but I've been searching and searching and so far I haven't found anything that relates to this specific issue.我觉得我一定错过了一些简单的东西,但我一直在搜索和搜索,到目前为止我还没有找到任何与这个特定问题相关的东西。 How do I get EF Core to actually send null
as the value for RelatedStepId
?如何让 EF Core 实际发送null
作为RelatedStepId
的值?
You shoud set RelatedStep
to null too.您也应该将RelatedStep
设置为 null。 I guess you didn't clear this object.我猜你没有清除这个 object。
Filling one of RelatedStepId
or RelatedStep
is enough for EF
to save the value and if you don't want it, you have to clear both of them.填充RelatedStepId
或RelatedStep
之一足以让EF
保存该值,如果您不想要它,则必须清除它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.