这是什么的EntityFramework code首先与财产的getter / setter方法​​呢?财产、方法、这是什么、EntityFramework

2023-09-05 23:10:42 作者:君临天下

这到底是什么的的EntityFramework做映射具有自定义getter和setter使用$ C C首先$当属性?

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

这仅仅反序列化时调用getter方法​​时,序列化的属性,而二传?所以,我可以做一些愚蠢的像...

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; }
}

注意我在使用上述code,或者类似的东西没有实际发生的利息......这,仅供参考。

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

此外,测绘与code物业时首先做的所有getter和setter需公开

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

推荐答案

是的; EF却会调用getter和setter。 它实际上是不可能的EF以任何其他的方式工作。

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)