如何在 Fluent NHibernate 中映射一对一关系。我已经尝试过其他一切
我有这个表结构,并想使用 Fluent Hibernate (如果可能的话,子类)来映射它。我无法更改结构,因为数据库记录太多,可能会导致主要应用程序返工。如果 Party 表中的 Id 是人员和组织表中的外键,那么会更容易,但在特定情况下,数据库将人员和组织键作为党表中的外键。任何帮助都会很棒。
参与方表
ID 人员ID OrganizationId
人员表
ID 名称 LName
组织表
ID 组织名称 组织描述
I have this table structure and would like to map it using Fluent Hibernate (subclass if possible). I cannot change the structure because the database has too many records and might cause major applications rework. It would be easier if the Id from Party table was a foreign key in person and organization table, but in the particular scenario the database has person and organization key as a foreign key in party table. Any help would be great.
Party table
Id
PersonId
OrganizationId
Person table
Id
FName
LName
Organization table
Id
OrgName
OrgDescription
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果链接表包含除构成复合 ID 的两个 ID 之外的任何字段,则无法使用自动映射来映射链接表。在您的情况下,您的 Party 表有一个 Id 字段,它违反了自动映射规则,因为它设计得不好(即复合 id 不应该有自动递增 id,尽管我们这样做是为了在我工作的地方建立索引)。
要解决此问题,您只需为 Party 创建一个 ClassMap,并将 Id 映射为 Id 并引用 Person 和 Organization。
然后,在您的 Person 和 Organization 实体中,您无需创建到 Party 实体的 HasManyToMany 映射,而是创建到 Party 实体的 HasMany。
实际上,您所做的是显式匹配代码,使其看起来更像 ERD,而自动映射仅在链接表仅包含复合主键时才通过链接表暗示多对多关系。
这困扰了我三天,我把这条路线当作“黑客”,后来几周前在 Fluent NHibernate 的 google 小组中读到了这个解释。
我希望这对您有所帮助,如果没有,我可以为您整理一些代码。
另请参阅我关于同样的情况。
编辑:
这是在相当高的水平上看起来的样子。请记住,您必须在实体构造函数中初始化集合并创建 setter 方法。请参阅入门
You can't map a linking table with the automapping if it contains any fields other than the two ids that make up the composite id. In your case, your Party table has an Id field which breaks the automapping rules because it's not well-designed (i.e. a composite id shouldn't have an auto-incrementing id, although we do this for indexing where I work).
To fix this, you'll just have to create a ClassMap for Party, and map Id as the Id and reference Person and Organization.
Then, in your Person and Organization entities, instead of creating a HasManyToMany mapping to Party, you'd create a HasMany to the Party entity.
Really, what you're doing is explicitly matching the code to look more like and ERD, whereas the automapping implies many-to-many relationships through a link table only if it contains only a composite primary key.
This stumped me for three days and I took this route as a "hack", only to later read this explanation in Fluent NHibernate's google group a couple of weeks ago.
I hope that helps, if not I can throw together some code for you.
See also my post about the same situation.
edit:
Here is how this would look at a fairly high level. Remember, you'll have to initialize your collections in your entity constructors and create setter methods. See Getting Started