0

我有4個表,定義如下:功能NHibernate - 如何映射到具有參考查找表表

Projects: 
    Project_Id 
    Project_Name 

Vendors: 
    Vendor_Id 
    Vendor_Name 

Project_Vendors: 
    Project_Vendor_Id 
    Project_Id 
    Vendor_Id 

Project_Vendor_Payments: 
    Payment_Id 
    Project_Vendor_Id 
    Payment_Amount 

我不知道從哪裏開始,甚至定義我的班級,功能NHibernate工作更不用說定義我的映射。

一個項目可以有許多供應商與之關聯,並且供應商可以爲每個項目接收許多付款。

有關我如何實現此目標的任何想法?

回答

4

我通過不引用我的查找表來解決這個問題,而是直接引用實體的外鍵列。

這裏是我的表結構:

Projects: 
    Project_Id 
    Project_Name 

Vendors: 
    Vendor_Id 
    Vendor_Name 

Project_Vendors: 
    Project_Vendor_Id 
    Project_Id 
    Vendor_Id 

Project_Vendor_Payments: 
    Payment_Id 
    Project_Id 
    Vendor_Id 
    Payment_Amount 

我的類被定義爲:

public class Project 
{ 
    public virtual int Id { get; set; } 
    public virtual string Name { get; set; } 
    public virtual IList<Vendor> Vendors { get; set; } 
    public virtual IList<VendorPayment> VendorPayments { get; set; } 
} 

public class Vendor 
{ 
    public virtual int Id { get; set; } 
    public virtual string Name { get; set; } 
} 

public class VendorPayment 
{ 
    public virtual int Id { get; set; } 
    public virtual Vendor Vendor { get; set; } 
    public virtual float Amount { get; set; } 
} 

我的映射:

public ProjectMappings : ClassMap<Project> 
{ 
    public ProjectMappings() 
    { 
     Table("Projects"); 
     Id(x => x.Id).Column("Project_Id"); 
     HasManyToMany(x => x.Vendors).Table("Project_Vendors") 
      .ParentKeyColumn("Project_Id") 
      .ChildKeyColumn("Vendor_Id") 
      .Cascade.AllDeleteOrphan(); 
     HasMany(x => x.VendorPayments).Table("Project_Vendor_Payments") 
      .KeyColumn("Project_Id") 
      .Cascade.AllDeleteOrphan(); 
     Map(x => x.Name).Column("Project_Name") 
    } 
} 

public class VendorMappings : ClassMap<Vendor> 
{ 
    public VendorMappings() 
    { 
     Table("Vendors"); 
     Id(x => x.Id).Column("Vendor_Id"); 
     Map(x => x.Name).Column("Vendor_Name"); 
    } 
} 

public class VendorPaymentMappings : ClassMap<VendorPayment> 
{ 
    public VendorPaymentMappings() 
    { 
     Table("Project_Vendor_Payments"); 
     Id(x => x.Id).Column("Payment_Id"); 
     References(x => x.Vendor).Column("Vendor_Id"); 
     Map(x => x.Amount).Column("Payment_Amount"); 
    } 
} 

這不是一個確切的答案,我問題,而只是解決問題的方法。仍在尋找如何完成問題的內容。