使用 Fluent 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 });
但是我的映射有问题。我确信我的问题围绕 DiscrimminateSubClassesOnColumn 位展开,但我对放在这里的内容有点迷失。如果有人能向我展示如何做到这一点,我将非常感激。谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不使用 Fluent,但字段不能同时是鉴别器和映射属性。
由于您使用的是继承 (
ProductTransaction
is-aTransaction
),因此您可以删除该属性 (Transaction
has-aTransactionType
)。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-aTransaction
), you can remove the property (Transaction
has-aTransactionType
).