实体框架 - 区分多个值
在按层次结构表的情况下,是否可以区分可能值的列表?
例如,对于 Color、DarkColor、LightColor
类型,
类似的
Map<DarkColor>(m => m.Requires("TheColor").HasValue(Red || Blue)
Map<LightColor>(m => m.Requires("TheColor").HasValue(Yellow || White)
例子很糟糕,但希望您能明白!
In a Table-Per-Hierachy scenario is it possible to discriminate on a list of possible values?
e.g. for the types Color, DarkColor, LightColor
something like
Map<DarkColor>(m => m.Requires("TheColor").HasValue(Red || Blue)
Map<LightColor>(m => m.Requires("TheColor").HasValue(Yellow || White)
poor example but hopefully you get the picture!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,这是不可能的。
实体框架仅支持使用条件交集而不是条件并集进行映射。
您可以在 设计器[1] 和EDMX语法[2].
* Code First 应具有相同的映射功能。
[2] 如果右键单击 edmx 文件并选择“打开方式...” --> “自动编辑器选择器(XML)”并尝试手动编辑映射条件,您将看到(从智能感知),没有在条件之间输入“OR”的选项。
No, it is not possible.
Entity framework only enables mapping using a intersection of conditions, not a union of conditions.
You can see this both in the designer[1] and in the EDMX syntax[2].
* Code first should have the same mapping capabilities.
[2] If you right click the edmx file and choose "Open With..." --> "Automatic Editor Selector (XML)" and try to manually edit the mapping conditions you will see (from the intellisense) that the is no option to enter "OR" between conditions.
首先,我不确定你想要的是否可行,EF想要处理鉴别器列,并且根据类类型,它想设置鉴别器值,在这种情况下,它会如何设置该值,为可能的值。从数据库加载它时没有什么区别,但在尝试将其序列化到数据库时有点问题。
1-您是否尝试过为每个可能的值执行多个映射:) 您可能会收到一条错误消息,指出 DarkColor 已被映射。
2-第二个建议是添加 [NotMapped] ColorWeight 属性,并根据 theColor 返回黑色或白色,并使用此属性作为鉴别器字段,但我猜鉴别器字段应该存在于表中。
First of all, I am not sure if what you want is possible, EF wants to take care of the discriminator column, and based on the class type, it would like to set the discriminator value, in this case, how is it going to set the value, to which possible one. It makes no difference when loading it from the DB, but a little problematic when trying to serialize it to the DB.
1- have you tried doing multiple maps for each possible value:) You might have an error saying DarkColor has already been mapped.
2- 2nd suggestion is adding a [NotMapped] ColorWeight attribute, and returning dark or white based on theColor, and using this property as the discriminator field, but I guess Discriminator field should exist in the Table.