我正在尝试为如下所示的数据库设置 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 表的列分别具有 Customer、SalesMan、Category 的主键作为外键。
活动与类别具有一对多关系
客户与类别具有多对多关系
Customer 与 SalesMan 具有多对多关系,但也与 Category 相关
即:为客户分配了特定类别的销售人员,因此 CustomerSalesManCategory 表中的正确条目应完美反映(所有 3 个 id 已填充)
使用我目前拥有的映射,将条目插入到 CustomerSalesManCategory 中,SalesMan_id 或 Category_id 为 NULLS,这对于我提供的映射是正确的。
但是,我无法弄清楚让 FluentNhibernate 正确完成工作的映射。在客户的 SaveOrUpdate 上的 CustomerSalesManCategory 表中的单个条目。
非常感谢任何帮助。这是我当前的映射:
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");
}
}