流利NHibernate的许多一对多的问题(Fluent Nhibernate many-to-ma

2019-10-30 11:36发布

我有三个表:

  1. 人(ID,名字)
  2. 组织(ID,姓名)
  3. PersonOrganization(PERSONID,OrganizationId,详细信息)许多一对多表

当我第一次映射这个使用功能NHibernate我没有在PersonOrganization表中的细节列,这个映射使用HasManyToMany在PersonMap和OrganizationMap(无需创建一个PersonOrganization域对象或地图)。 我可以writethe下面的代码:

Organization org = new Organization { Name = "org" };
People people = new People { FirstName = "firstname", Organization = org };

peopleRepository.Add(people);          // ISession.Save(people)
unitOfWork.Commit();                   // ITransaction.Commit()

NHhibernate高高兴兴数据致力于为所有三个表。

问题来了,当我在PersonOrganization表添加的细节列。 经过一番研究,事实证明,我现在要创建一个新的PersonOrganization域对象和映射和设置两个人物和组织一个的hasMany关系。 我更新的模型及以下地图:

public class People
{
    public People()
    {
        LinkToOrganization = new List<PeopleOrganization>();
    }

    public virtual int Id { get; set; }
    public virtual string FirstName { get; set; }
    public virtual IList<PeopleOrganization> LinkToOrganization { get; set; }
}

public class Organization
{
    public Organization()
    {
        LinkToPeople = new List<PeopleOrganization>();
    }

    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
    public virtual IList<PeopleOrganization> LinkToPeople { get; set; }
}

public class PeopleOrganization
{
    public virtual People People { get; set; }
    public virtual Organization Organization { get; set; }
    public virtual string Details { get; set; }
}

public class PeopleMap : ClassMap<People>
{
    public PeopleMap()
    {
        Id(p => p.Id);
        Map(p => p.FirstName).Length(100);

        HasMany(x => x.LinkToOrganization)
            .Table("PeopleOrganization")
            .KeyColumn("PeopleId")
            .AsBag()
            .Inverse();
    }
}

public class OrganizationMap : ClassMap<Organization>
{
    public OrganizationMap()
    {
        Id(p => p.Id);
        Map(p => p.Name).Length(50);

        HasMany(x => x.LinkToPeople)
            .Table("PeopleOrganization")
            .KeyColumn("OrganizationId")
            .AsBag()
            .Inverse();
    }
}

public class PeopleOrganizationMap : ClassMap<PeopleOrganization>
{
    public PeopleOrganizationMap()
    {
        CompositeId()
            .KeyReference(p => p.People, "PeopleId")
            .KeyReference(p => p.Organization, "OrganizationId");

        Map(p => p.Details).Length(100);
    }
}

我现在已经写了下面的代码:

People people = new People { FirstName = "firstname" };
Organization org = new Organization { Name = "org" };
PeopleOrganization po = new PeopleOrganization { People = people, Organization = org, Details = "details" };

peopleRepository.Add(people);          // ITransaction.Begin() ISession.Save(people)
organizationRepository.Add(org);       // ISession.Save(org)
peopleOrganizationRepository.Add(po);  // ISession.Save(po)

unitOfWork.Commit();                   // ITransaction.Commit()

我的问题是:

  1. 是我的映射正确设置,支持这种多到许多情景?
  2. 是否有办法,我只能够做以下(这将写信给所有三个表):

-

People people = new People { FirstName = "firstname" };
Organization org = new Organization { Name = "org" };
PeopleOrganization po = new PeopleOrganization { People = people, Organization = org, Details = "details" };

peopleRepository.Add(people);          // Just ONE ISession.Save(people)

unitOfWork.Commit();                   // ITransaction.Commit()

任何输入被高度赞赏。

谢谢

Answer 1:

首先,您需要将新创建的添加PeopleOrganization对两个实体(集合OrganizationPeople )。 然后,如果你添加一个Cascade.All()到您HasMany链上,节省了将传播(级联)下降到PeopleOrganization并随后Organization

作为一个纯粹的语义建议,我建议封装PeopleOrganization创建成一个方法Person 。 事情是这样的:

class Person
{
  public void AddOrganization(Organization org, string details)
  {
    var link = new PeopleOrganization { ... };

    LinkToOrganization.Add(link)
    org.LinkToPeople.Add(link);
  }
}

这样,你永远不会有对付自己创建的intrim实体。



文章来源: Fluent Nhibernate many-to-many issue