EF 6: mapping complex type collection?

若如初见. 提交于 2020-01-13 09:59:05

问题


Does EF 6 (code first) supports complex type collection(Value Object collections) mappings? I know that it supports Complex types, but haven't still found an example where we have a collection of complex types.

For instance, suppose you have an entity called Student, which has a collection of contacts. With NH, I can simply say that Student has a collection of contacts and that contact is a component (equivalent to complex type in ef). Can this be done with EF without changing contact to an entity?


回答1:


Apparently NHibernate is more flexible in that regard, because at the time of writing (EF6.2 and EF Core 2.1), neither EF6 nor EF Core support complex (or more generally primitive or value object) type collection mapping.

EF Core is even worse because the Owned Entity Types, supposedly to be the replacement of the EF Complex Types really have more entity like behavior (from the change tracking perspective) and cannot be used as let say DDD immutable multi property value objects.

The only workaround I know is to map the value object/collection representation in some serialization format ((e.g. XML, JSON, binary etc.) to a single database string/binary column. While this would work for reading/storing the data, it lacks the query capabilities, so IMO is not a serious option.

Speaking about EF6, I don't think it will ever get that support. EF6 is basically in maintenance mode and won't get future major improvements.

EF Core looks more promising in that regard because the Support for collections of owned entities is scheduled for the next EF 2.2 release. However it's unknown how they will implement them (initially), and taking into account how they did the owned types, it might not be the way you expect, so it can't be said in advance if they would work for value object collection scenarios.

I know this is not the answer you wanted, but that's the reality.




回答2:


Complex types are mapped to the entity's table by just adding a column for each property in the complex type. So, with Contact as a separate entity:

public class Student
{
    [Key]
    public int ID {get; set;}
    public Contact PrimaryContact { get; set; }
    public Contact SecondaryContact{ get; set; }
}

[ComplexType]
public class Contact
{
    public string Address{get; set;}
    public string PhoneNumber{get; set;}
    public string Email{get; set;}
}

Will yield a mapping of a Student table that has columns:

ID
PrimaryContact_Address
PrimaryContact_PhoneNumber
PrimaryContact_Email
SecondaryContact_Address
SecondaryContact_PhoneNumber
SecondaryContact_Email

Complex types are just shorthand for declaring the same members wherever you need it. You could use that same Contact type in a bunch of other entities that need contact data without having to explicitly define Address, PhoneNumber, and Email properties for each entity. So you can't really use them in a collection, because every time you wanted to add or remove items to it, you'd have to add or remove columns from the table itself.

public class Student
{
    [Key]
    public int ID {get; set;}
    public ICollection<Contact> Contacts{ get; set; }
}

[ComplexType]
public class Contact
{
    public string Address{get; set;}
    public string PhoneNumber{get; set;}
    public string Email{get; set;}
}

ID
Contacts[x]_Address?
Contacts[x]_PhoneNumber?
Contacts[x]_Email?

Where could Contacts items actually be stored? How could you index it? If you try to do this, the mapper would just ignore the Contacts property altogether.

You can cleanly use collections by just removing the ComplexType from the Contact class. This would create a table in the database, though:

public class Student
{
    [Key]
    public int ID {get; set;}
    public ICollection<Contact> Contacts{ get; set; }
}

public class Contact
{
    [Key]
    public int ID {get; set;}
    public string Address{get; set;}
    public string PhoneNumber{get; set;}
    public string Email{get; set;}
}


来源:https://stackoverflow.com/questions/29878961/ef-6-mapping-complex-type-collection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!