如何在 Fluent NHibernate 中映射一对一关系。我已经尝试过其他一切

发布于 2024-08-29 06:34:00 字数 271 浏览 7 评论 0原文

我有这个表结构,并想使用 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 技术交流群。

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

发布评论

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

评论(1

情感失落者 2024-09-05 06:34:01

如果链接表包含除构成复合 ID 的两个 ID 之外的任何字段,则无法使用自动映射来映射链接表。在您的情况下,您的 Party 表有一个 Id 字段,它违反了自动映射规则,因为它设计得不好(即复合 id 不应该有自动递增 id,尽管我们这样做是为了在我工作的地方建立索引)。

要解决此问题,您只需为 Party 创建一个 ClassMap,并将 Id 映射为 Id 并引用 Person 和 Organization。

然后,在您的 Person 和 Organization 实体中,您无需创建到 Party 实体的 HasManyToMany 映射,而是创建到 Party 实体的 HasMany。

实际上,您所做的是显式匹配代码,使其看起来更像 ERD,而自动映射仅在链接表仅包含复合主键时才通过链接表暗示多对多关系。

这困扰了我三天,我把这条路线当作“黑客”,后来几周前在 Fluent NHibernate 的 google 小组中读到了这个解释。

我希望这对您有所帮助,如果没有,我可以为您整理一些代码。

另请参阅我关于同样的情况

编辑:

这是在相当高的水平上看起来的样子。请记住,您必须在实体构造函数中初始化集合并创建 setter 方法。请参阅入门

public class Party {
    public virtual int Id { get; set; }
    public virtual Person Person { get; set; }
    public virtual Organization Organization { get; set; }
}
public class PartyMap : ClassMap<Party> {
    public PartyMap() {
        Id(x => x.Id);
        References(x => x.Person);
        References(x => x.Organization);
    }
}
public class Person {
    public virtual int Id { get; set; }
    public virtual string FName { get; set; }
    public virtual string LName { get; set; }
    public virtual ICollection<Party> Parties { get; set; }
}
public class PersonMap : ClassMap<Person> {
    public PersonMap() {
        Id(x => x.Id);
        Map(x => x.FName);
        Map(x => x.LName);
        HasMany(x => x.Parties);
    }
}
public class Organization {
    public virtual int Id { get; set; }
    public virtual string OrgName { get; set; }
    public virtual string OrgDescription { get; set; }
    public virtual ICollection<Party> Parties { get; set; }
}
public class OrganizationMap : ClassMap<Organization> {
    public OrganizationMap() {
        Id(x => x.Id);
        Map(x => x.OrgName);
        Map(x => x.OrgDescription);
        HasMany(x => x.Parties);
    }
}

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

public class Party {
    public virtual int Id { get; set; }
    public virtual Person Person { get; set; }
    public virtual Organization Organization { get; set; }
}
public class PartyMap : ClassMap<Party> {
    public PartyMap() {
        Id(x => x.Id);
        References(x => x.Person);
        References(x => x.Organization);
    }
}
public class Person {
    public virtual int Id { get; set; }
    public virtual string FName { get; set; }
    public virtual string LName { get; set; }
    public virtual ICollection<Party> Parties { get; set; }
}
public class PersonMap : ClassMap<Person> {
    public PersonMap() {
        Id(x => x.Id);
        Map(x => x.FName);
        Map(x => x.LName);
        HasMany(x => x.Parties);
    }
}
public class Organization {
    public virtual int Id { get; set; }
    public virtual string OrgName { get; set; }
    public virtual string OrgDescription { get; set; }
    public virtual ICollection<Party> Parties { get; set; }
}
public class OrganizationMap : ClassMap<Organization> {
    public OrganizationMap() {
        Id(x => x.Id);
        Map(x => x.OrgName);
        Map(x => x.OrgDescription);
        HasMany(x => x.Parties);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文