What is a complex type in entity framework and when to use it?

后端 未结 2 681
名媛妹妹
名媛妹妹 2020-12-28 14:12

I have tried to read the msdn article on complex types. But it does not explain when to use it. Also there is not a comprehensive explanation on the web on complex types and

2条回答
  •  隐瞒了意图╮
    2020-12-28 14:28

    Consider this ContactDetails class for example:

    public class ContactDetails
    {
        public string HomePhone { get; set; }
        public string MobilePhone { get; set; }
        public string FaxNumber { get; set; }
    }
    

    By default, EF will treat ContactDetails as an Entity. That means that if (for example) you're having a Person class with a navigation-property of ContactDetails type, EF will map the Person.ContactDetails relationship to a different table (because Entity is something that is having an identity of its own, hence other entities may refer to it - and that would require a different table in relational terms).

    By denoting ContactDetails as a Complex Type instead, EF will no longer treat it as an entity that requires a relationship and instead map it to the same table of the parent (containing) entity (Person in my example), effectively making it a Value Object.

提交回复
热议问题