代码之家  ›  专栏  ›  技术社区  ›  nfplee

用fluent nhibernate映射父表和子表

  •  1
  • nfplee  · 技术社区  · 14 年前

    嗨,在我的数据库中定义了以下表:

    Transactions:
    - TransactionID (PK, Identity)
    - TypeID (FK)
    - Amount
    
    TransactionTypes:
    - TypeID (PK, Identity)
    - Type
    
    ProductTransactions:
    - TransactionID (PK)
    - Discount
    

    有两种类型的事务(事件和产品)。产品具有附加折扣字段,因此定义了附加表。

    我现在有以下实体:

    public class Transaction
    {
        public virtual int TransactionID { get; private set; }
        public virtual TransactionType Type { get; set; }
        public virtual decimal Amount { get; set; }
    }
    
    public class ProductTransaction : Transaction
    {
        public virtual decimal Discount { get; set; }
    }
    
    public enum TransactionType
    {
        Event = 1,
        Product = 1
    }
    

    最后,我的映射如下:

    public TransactionMap()
    {
        Table("Transactions");
        Id(x => x.TransactionID);
        Map(x => x.Amount);
        DiscriminateSubClassesOnColumn("TypeID")
            .SubClass<ProductTransaction>(TransactionType.Product, x => x.References(y => y.Type));
    }
    
    public ProductTransactionMap()
    {
        Table("ProductTransactions");
        Map(x => x.Discount);
    }
    

    我想说以下内容来插入产品交易:

    productRepository.Insert(new ProductTransaction { Type = TransactionType.Product, Amount = 100m, Discount = 10m });
    

    但是我的地图有问题。我确信我的问题是围绕distingSubclassesonColumn位的,但是我有点迷失在这里。如果有人能告诉我怎么做,我会非常感激的。谢谢

    1 回复  |  直到 14 年前
        1
  •  0
  •   Diego Mijelshon    14 年前

    我不使用fluent,但字段不能既是鉴别器又是映射属性。

    因为你在使用继承( ProductTransaction IS-A Transaction )可以删除属性( 交易 HAS-A TransactionType )