当为带有DB NULL的列的实体重新补水时,Entity Framework是否使用null调用属性设置器?

问题描述

我有一个字符串属性

public string XYZ 
{ 
     get => // do stuff
     set => // do stuff which handles null
}

因为我希望它会被调用。...

但是真的吗? (EF6.4)

解决方法

似乎会。如果使用后备字段实现该属性,则可以通过在设置器中放置一个断点来进行测试。 EG

private string xyz;
public string XYZ
{
    get
    {
        return xyz;
    }
    set
    {
        xyz = value;
    }
}

我想必须这样做,因为EF不知道您的实体是否具有属性的非标准默认值。例如,你可以写

private string xyz = "none";
public string XYZ
{
    get
    {
        return xyz;
    }
    set
    {
        xyz = value;
    }
}

因此,水合代码需要运行setter才能获得正确的结果。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...