Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does EntityFramework Code First do with property getters/setters?

What exactly does the EntityFramework do to map properties that have custom getters and setters when using Code First?

Does it simply call the getter for a property when serializing, and the setter when deserializing? So I could do something silly like...

public class Foo {

    public DateTime TimeAccessed {
        get {
            return DateTime.Now;
        }
        set {
            TimeDeserialized = DateTime.Now;
        }
    }

    [NotMapped]
    public DateTime TimeDeserialized { get; private set; }
}

Note I have no actual interest in using the above code, or anything like it... it's for illustrative purposes only.

Also, when mapping a property with Code First, do all getters and setters need to be public?

like image 758
Eric Avatar asked Aug 15 '12 00:08

Eric


1 Answers

Yes; EF does call the getters and setters.
It would actually be impossible for EF to work in any other way.

No; they can even be private. (although the property itself must be public)

like image 196
SLaks Avatar answered Sep 19 '22 22:09

SLaks