NHibernate 使用公共属性映射到实体

发布于 2024-10-01 12:28:37 字数 1831 浏览 0 评论 0原文

我正在使用 IQueryable<>建立批处理查询。

我已成功使用视图来获取信息,因此 IQueryable<>可以找到它,但在这种情况下我无法弄清楚如何映射视图,因为它取决于属性而不是实体的 ID。

假设我有这个实体和映射:

public class Calculation 
{
    public virtual int Id { get; set; }
    public virtual Organisation Organisation { get; set; }
    public virtual Charge Charge { get; set; }
    public virtual TransactionTotal TransactionTotal { get; set; }
}

public class CalculationMap : ClassMap<Calculation>
{
    public CalculationMap()
    {
        Id(x => x.Id).GeneratedBy.Identity();
        References(x => x.Organisation).Not.Nullable().UniqueKey("OC");
        References(x => x.Charge).Not.Nullable().UniqueKey("OC");
    }

这是我需要进入的类:我正在使用一个视图来为我提供每个组织和费用的总金额:

public class TransactionTotal 
{
    public virtual int Id { get; set; }
    public virtual Organisation Organisation { get; set; }
    public virtual Charge Charge { get; set; }
    public virtual decimal Amount { get; set; }        
}

public class TransactionTotalMap : ClassMap<TransactionTotal>
{
    public TransactionTotalMap()
    {
        Id(x => x.Id).GeneratedBy.Identity();
        Table("TransactionTotalsView");
        References(x => x.Charge).Not.Nullable();
        References(x => x.Organisation).Not.Nullable();
        Map(x => x.Amount).Precision(15).Scale(2).Not.Nullable();
    }
}

我使用过视图的其他地方我已经成功使用了诸如 HasOne(x => x.TransactionTotal); 但在本例中我需要告诉 Nhibernate 使用 Organization 和 Charge 属性作为键。

这可能吗?如果是这样,如何将 TransactionTotal 类映射到 Calculation 类?

编辑:我已经按照 @David 的建议在 TransactionTotalMap 中使用了 CompositeId:

CompositeId().KeyProperty(x => x.Organisation.Id).KeyProperty(x => x.Charge.Id);

不过,我仍然不知道要在 CalculationMap 中放入什么。

I am using IQueryable<> to build up batching queries.

I have used views successfully to fetch information so the IQueryable<> can find it, but in this instance I can't work out how to map a view, as it depends on properties rather than the entity's ID.

Say I have this entity and mapping:

public class Calculation 
{
    public virtual int Id { get; set; }
    public virtual Organisation Organisation { get; set; }
    public virtual Charge Charge { get; set; }
    public virtual TransactionTotal TransactionTotal { get; set; }
}

public class CalculationMap : ClassMap<Calculation>
{
    public CalculationMap()
    {
        Id(x => x.Id).GeneratedBy.Identity();
        References(x => x.Organisation).Not.Nullable().UniqueKey("OC");
        References(x => x.Charge).Not.Nullable().UniqueKey("OC");
    }

This is the class I need to get in there: I'm using a view to give me the total amount per Organisation and Charge:

public class TransactionTotal 
{
    public virtual int Id { get; set; }
    public virtual Organisation Organisation { get; set; }
    public virtual Charge Charge { get; set; }
    public virtual decimal Amount { get; set; }        
}

public class TransactionTotalMap : ClassMap<TransactionTotal>
{
    public TransactionTotalMap()
    {
        Id(x => x.Id).GeneratedBy.Identity();
        Table("TransactionTotalsView");
        References(x => x.Charge).Not.Nullable();
        References(x => x.Organisation).Not.Nullable();
        Map(x => x.Amount).Precision(15).Scale(2).Not.Nullable();
    }
}

Other places I have used views I have successfully used mappings like HasOne(x => x.TransactionTotal); but in this instance I need to tell Nhibernate to use the Organisation and Charge properties as the key.

Is this even possible? If so, how do I map the TransactionTotal class to the Calculation class?

Edit: I have used CompositeId in TransactionTotalMap as suggested by @David:

CompositeId().KeyProperty(x => x.Organisation.Id).KeyProperty(x => x.Charge.Id);

I'm still stuck on what to put in the CalculationMap though.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

哆兒滾 2024-10-08 12:28:37

使用 CompositeId() 方法在你的映射中

use the CompositeId() method in your mapping

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