NHibernate mapping for an object using common properties

I use IQueryable <> to create batch queries.

I have successfully used the views to retrieve the information, so IQueryable <> can find it, but in this case I can’t determine how to map the view, since it depends on the properties and not on the identifier of the object.

Let's say that 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: I use the view to give me the total amount for organization and fee:

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 that I used in the view, I successfully used mappings, such as HasOne(x => x.TransactionTotal);, but in this case I need to tell Nhibernate to use the Organization and Charge properties as the key.

? , TransactionTotal Calculation?

: CompositeId TransactionTotalMap, @David:

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

, CalculationMap.

+3
1
+1

Source: https://habr.com/ru/post/1774067/


All Articles