在多个多对多关系中表的流畅NHibernate映射

时间:2012-04-18 14:34:10

标签: fluent-nhibernate fluent-nhibernate-mapping

我正在尝试为下面显示的数据库设置Fluent NHibernate映射:

 Campaign Table
 Id
 Name

Category Table
Id  
Campaign_id
Name

Customer Table
Id
Name
Phone
...

SalesMan Table
Id
Name
Phone
...

CustomerSalesManCategory Table
Id
Customer_id
SalesMan_id
Category_id

CustomerSalesManCategory表的列分别作为foreignKeys的Customer,SalesMan,Category的主键。

广告系列与类别

具有一对多的关系

客户与类别

有多对多的关系

客户与SalesMan有多对多的关系,但也与类别

有关

即:为特定类别的客户分配了一个SalesMan,因此CustomerSalesManCategory表中的正确条目应该完全反映出(所有3个ID都已填充)

使用我当前的映射,条目将插入CustomerSalesManCategory中,并在SalesMan_id或Category_id上使用NULLS,这对于我提供的映射是正确的。

但是,我无法弄清楚让FluentNhibernate正确地完成工作的问题。在CustomerOalesUCateory表中,在CustomerOrUpdate for Customer上单独使用。

任何帮助都非常有用。 这是我目前的映射:

 public class CampaignMap : ClassMap<Campaign>
{
    public CampaignMap()
    {
        Id(x => x.Id);
        Map(x => x.Name);
        Map(x => x.CreationDate);
        HasMany(x => x.CampaignsCategories).Inverse().Cascade.AllDeleteOrphan();
    }
}

  public class CategoryMap : ClassMap<Category>
{
    public CategoryMap()
    {
       Id(x => x.Id);
       Map(x => x.Extension);
       References(x => x.Campaign);
       HasManyToMany(x =>         x.CustomersAssginedInCategory).Inverse().Table("CustomerSalesManCategory");           
    } 
}

public class CustomerMap : ClassMap<Customer>
{
    public CustomerMap()
    {
        Id(x => x.Id);
        Map(x => x.CallingName);
        Map(x => x.OfficialName);
        Map(x => x.Year);
        Map(x => x.Description);
        HasManyToMany(x => x.CustomerCategories).Table("CustomerSalesManCategory");
        HasManyToMany(x => x.CustomerSalesMen).Table("CustomerSalesManCategory");
    }
}

 public class SalesManMap : ClassMap<SalesMan>
{
    public SalesManMap()
    {
        Id(x => x.Id);
       Map(x => x.Name);
       Map(x => x.Email);
       Map(x => x.Phone);
       Map(x => x.Description);
       HasManyToMany(x => x.CustomersAssignedTo).Inverse().Table("CustomerSalesManCategory");           
    }
}  

1 个答案:

答案 0 :(得分:0)

三向关联通常是字典

// in Customer

public IDictionary<SalesMan, Category> asd { get; private set; }

// in CustomerMap
HasManyToMany(x => x.CustomerSalesMen)
    .Table("CustomerSalesManCategory")
    .AsEntityMap("SalesManId");