EF 6:映射复杂类型集合? [英] EF 6: mapping complex type collection?

查看:202
本文介绍了EF 6:映射复杂类型集合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

EF 6(代码优先)是否支持复杂类型集合(值对象集合)映射?我知道它支持复杂类型,但是还没有找到一个包含复杂类型集合的示例.

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.

例如,假设您有一个名为Student的实体,该实体具有联系人集合.有了NH,我可以简单地说出Student拥有一个联系人集合,并且该联系人是一个组成部分(等效于ef中的复杂类型).可以使用EF来完成,而无需更改与实体的联系吗?

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?

推荐答案

显然,NHibernate在这方面更加灵活,因为在编写本文时(EF6.2和EF Core 2.1),EF6和EF Core都不支持复杂的(或更一般的原始或值对象)类型集合映射.

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更加糟糕,因为拥有的实体类型,据称是EF复杂类型的替代,实际上具有更多类似行为的实体(从更改跟踪的角度来看),因此不能用作DDD不可变的多属性值对象.

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.

我知道的唯一解决方法是将某种序列化格式(例如XML,JSON,二进制等)的值对象/集合表示映射到单个数据库字符串/二进制列.数据,它缺乏查询功能,因此IMO并不是一个严肃的选择.

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.

说到EF6,我认为它永远不会得到这种支持. EF6基本处于维护模式,并且不会有未来重大改进.

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看起来更有希望,因为对拥有实体集合的支持

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.

这篇关于EF 6:映射复杂类型集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆