【问题标题】:Fluent NHibernate - How to map to table which has reference to a lookup tableFluent NHibernate - 如何映射到引用查找表的表
【发布时间】:2012-11-28 19:56:57
【问题描述】:

我在下面定义了 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,更不用说定义我的映射了。

一个项目可以有许多与之关联的供应商,一个供应商可以在每个项目中收到许多付款。

关于如何实现这一点的任何想法?

【问题讨论】:

    标签: c# fluent-nhibernate asp.net-mvc-4 fluent-nhibernate-mapping


    【解决方案1】:

    我通过不引用我的查找表而只使用直接引用实体的外键列解决了这个问题。

    这是我的表结构:

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

    这不是我的问题的确切答案,而只是解决问题的方法。仍在寻找如何解决问题中的问题。

    【讨论】:

      猜你喜欢
      • 2011-03-25
      • 2012-02-17
      • 1970-01-01
      • 2010-12-25
      • 2011-04-10
      • 1970-01-01
      • 1970-01-01
      • 2011-08-12
      • 2012-04-16
      相关资源
      最近更新 更多