Does Entity Framework call property setters with null when rehydrating entities for columns with DB NULL?

44 Views Asked by At

I have a string property

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

because I'm hoping it will get called....

But will it really? (EF6.4)

1

There are 1 best solutions below

0
On BEST ANSWER

It appears it will. If you implement the property with a backing field it's easy to test this by putting a breakpoint in the setter. EG

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

And I think it would have to, as EF does not know whether your entities have a non-standard default value for a property. eg you could write

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

And so the hydration code would need to run the setter to get a correct result.