簡體   English   中英

流利的NHibernate-如何映射到引用了查詢表的表

[英]Fluent NHibernate - How to map to table which has reference to a lookup table

我在下面定義了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_Vendor_Id
    Payment_Amount

我不確定從哪里開始定義我的類以與Fluent 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_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");
    }
}

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM