在 Fluent nHibernate 中保存基类列表
我是一个 Fluent nHibernate 新手,所以请原谅我,这很简单,但我只是想念它。
我有一个包含 Customer 和 User 的类层次结构,它们都继承自 Person。客户和用户都有自己的表和映射,一切都运行良好。
我的问题是客户和用户需要有一个联系人列表,其类型为 IList。因此,客户和用户将各自拥有一个可以包含客户和用户混合的联系人列表。
我对如何绘制此图感到有点困惑,并且非常感谢您提供的任何建议。
谢谢!
I'm a bit of a Fluent nHibernate newbie, so forgive me is this is easy and I'm just missing it.
I have a class hierarchy with Customer and User which both inherit from Person. Customer and User each have their own table and mapping and everything is working great.
My problem is that Customer and User need to have a list of Contacts, which will be of type IList. So Customer and Users will each have a list of contact that can contain a mix of Customers and Users.
I'm kinda stumped on how to map this and would appreciate any advice that is offered.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我相信您需要一个
IList联系 {get;set;}
和类型(鉴别器),以便您知道将其转换为什么。也许这是一个黑客,但我就是这么做的。编辑:
假设您的实体如下所示。
解决问题的简单(hacky)方法是使用鉴别器并将所有内容移至同一张表。其映射可能如下所示:
如果您根据该模型生成模式,最终会得到:
我确实知道这可能并不理想,所以如果您的老板(像我的)是拒绝离开数据库的数据库专家在这样的状态下。然后,您需要做的就是删除
DiscriminateSubClassesOnColumn("TypeOfPerson");
,如果您从代码中生成架构,则最终应该得到如下所示的表结构:使用最后一个架构,您可以如果您需要 IList,则需要找出一种方法来确定所选 Person 到底是哪个子类,但我相信您可以弄清楚。 :) 希望这有帮助!
I believe you need a
IList<Person> Contacts {get;set;}
and a type (discriminator) so that you know what to cast it to. Maybe it's a hack but that's how I would do it.EDIT:
Let's say your entities would look like below.
The easy (hacky) way of solving your problem would be a discriminator and move everything to the same table. The mapping for this could look like:
If you generate your schema based on that model you end up with:
I do understand that this is maybe not ideal certainly so if your boss (like mine) is a database guru that refuses to leave the database in such a state. All you need to do then is to remove the
DiscriminateSubClassesOnColumn("TypeOfPerson");
and you should instead end up with a table structure like below should you generate the schema from your code:With this last schema you need to figure out a way to determine which subclass the selected Person really is if you ever need an
IList<Person>
but I am sure you can figure that out. :) Hope this helps!我是按照党的模式做到这一点的。这样,您的抽象始终是 Party:
对于映射,有两种可能的策略:每个继承树一个表;每班一张桌子。
这应该创建 4 个表:Party、Person、Organization 和 Contact
I have done this by Party model. This way your abstraction is always Party:
For mapping there are two possible strategies: One table per inheritance tree; one table per class.
That should create 4 tables: Party, Person, Organization and Contact