简体   繁体   English

NHibernate.Linq - 自定义/计算属性表达式

[英]NHibernate.Linq - Custom/Calculated property expression

How can a domain object include a property that calculates a value from other database mapped properties so that the calculated property can be used in both the domain object instance and the db level by nhibernate.linq. 域对象如何包含从其他数据库映射属性计算值的属性,以便nhibernate.linq可以在域对象实例和数据库级别中使用计算属性。

I would like to be able to use the property when working with the object directly: 我希望能够在直接使用对象时使用该属性:

Console.WriteLine(Entity.Calculated.ToString());

And when working with nhibernate.linq 当使用nhibernate.linq时

var q = from e in session.Linq<Entity>()
                where e.Calculated > 0
                select e;

You need to duplicate the logic in the class and the mapping. 您需要复制类和映射中的逻辑。 Here's an example: 这是一个例子:

Class: 类:

public class Invoice
{
    public virtual int Id { get; protected set; }
    public virtual decimal Amount { get; set; }
    public virtual decimal Paid { get; set; }
    public virtual decimal Balance
    {
        get { return Amount - Paid; }
    }
}

Mapping: 制图:

<class name="Invoice">
    <id name="Id">
        <generator class="hilo"/>
    </id>
    <property name="Amount"/>
    <property name="Paid"/>
    <property name="Balance" formula="Amount - Paid" access="readonly"/>
</class>

Now you can use Linq and query on Invoice.Balance 现在,您可以在Invoice.Balance上使用Linq和查询

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM