2010-08-05 117 views
1

喜映射父母与子女表在我的数据库中定义下表:与功能NHibernate

Transactions: 
- TransactionID (PK, Identity) 
- TypeID (FK) 
- Amount 

TransactionTypes: 
- TypeID (PK, Identity) 
- Type 

ProductTransactions: 
- TransactionID (PK) 
- Discount 

有2种类型的交易(活动和产品)的。产品有一个额外的折扣字段,所以定义了一个额外的表格。

我现在有以下实体:

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

但是我的映射有问题。我敢肯定,我的问题围绕着DiscriminateSubClassesOnColumn位进行,但我对于放在这里的东西不知所措。如果有人能告诉我如何做到这一点,我会非常感激。谢谢

回答

0

我不使用流利,但一个字段不能既是鉴别器也是映射属性。

由于使用的继承(ProductTransaction是,一个Transaction),你可以删除属性(Transaction有-一个TransactionType)。

+0

嗨,欢呼,似乎我过了一些复杂的事情。现在全部排序。 – nfplee 2010-08-05 20:56:41