简体   繁体   中英

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

I have 4 tables defined below:

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

I'm not sure where to begin even to define my classes to work with Fluent NHibernate let alone to define my mappings.

A project can have many vendors associated with it, and a vendor can receive many payments per project.

Any ideas on how I can make this happen?

I solved this problem by not referencing my lookup table and instead just having foreign key columns which reference the entities directly.

Here is my table structure:

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

My classes are defined as:

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; }
}

And my mappings:

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");
    }
}

This isn't an exact answer to my question, but rather just a solution to the problem. Still looking for how to do exactly what was in the question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM