实体框架 - 区分多个值

发布于 2024-11-05 04:40:54 字数 306 浏览 4 评论 0原文

在按层次结构表的情况下,是否可以区分可能值的列表?

例如,对于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

骄傲 2024-11-12 04:40:54

不,这是不可能的。

实体框架仅支持使用条件交集而不是条件并集进行映射。
您可以在 设计器[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.

心不设防 2024-11-12 04:40:54

首先,我不确定你想要的是否可行,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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文