views:

53

answers:

1

Hi have the following tables defined in my database:

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

TransactionTypes:
- TypeID (PK, Identity)
- Type

ProductTransactions:
- TransactionID (PK)
- Discount

There are 2 types of transactions (Events and Products). Products have an additional Discount field so an additional table is defined.

I now have the following entities:

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
}

Finally my mappings are as follows:

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

I'd like to be able to say the following to insert a product transaction:

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

However there is something wrong with my mapping. I'm sure my problem revolves around the DiscriminateSubClassesOnColumn bit but i'm abit lost with what to put here. I'd really appreciate it if someone could show me how this could be done. Thanks

A: 

I don't use fluent, but a field can't be both a discriminator and a mapped property.

Since you are using inheritance (ProductTransaction is-a Transaction), you can remove the property (Transaction has-a TransactionType).

Diego Mijelshon
Hi, cheers it seems i was over complicating things a little. All sorted now.
nfplee