使用 Fluent NHibernate 映射父表和子表

发布于 2024-09-13 09:39:06 字数 1358 浏览 3 评论 0原文

您好,我的数据库中定义了下表:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青朷 2024-09-20 09:39:06

我不使用 Fluent,但字段不能同时是鉴别器和映射属性。

由于您使用的是继承 (ProductTransaction is-a Transaction),因此您可以删除该属性 (Transaction has-a TransactionType)。

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文