【问题标题】:using a calculated property (not mapped) in Nhibernate QueryOver在 Nhibernate QueryOver 中使用计算属性(未映射)
【发布时间】:2016-10-07 02:07:26
【问题描述】:

我有一个表 A,它有以下列:

AId - TargetId - IsActive

对应于这张表,我有下面的类(带有额外的计算属性)和映射器:

public class A 
{
    public virtual long AId { get; set; }
    public virtual int TargetId { get; set; }
    public virtual int IsActive { get; set; }

    //calculated property, doesn't exist in the table
    public virtual bool IsClientSide
    {
        get { return ((this.TargetId & TargetEnum.ClientSide) != 0); }
    }
}

using NHibernate.Mapping.ByCode;
using NHibernate.Mapping.ByCode.Conformist;
using NHibernate.Type;
using ANamespace.A;

namespace mapping 
{
    public class AMap : ClassMapping<A> 
    {
        public AMap() 
        {
            this.Cache(x => { x.Usage(CacheUsage.NonstrictReadWrite); x.Region("LongTerm"); x.Include(CacheInclude.All); });
            this.Lazy(false);
            this.Mutable(true);
            this.DynamicUpdate(true);
            this.Id(x => x.AId, map => map.Generator(Generators.Native));
            this.Property(x => x.TargetId, map => { map.NotNullable(true); map.Type<EnumType<TargetEnum>>(); });
            this.Property(x => x.IsActive, map => map.NotNullable(true));
        }
    }
}

我没有映射这个 IsClientSide 属性,因为它不在表中。但我想在我的查询中使用它,如下所示:

A aobject = null;
alist = session.QueryOver(() => aobject)

        .Where(a => a.IsClientSide)
        .And(tt => a.IsActive)
        ......

我找到了 Hendry Luk 的“Linq-ing Calculated Properties”帖子,但 ILinqToHqlGenerator 使用这个小属性似乎过于复杂。

我怎样才能在我的映射器类中制定 IsClientSide 属性?

【问题讨论】:

    标签: c# sql linq nhibernate


    【解决方案1】:

    我们需要 1) Formula 映射和 2) bitwise operator

    Property(x => x.IsClientSide, map =>
    {
        map.Formula("(Target_ID & 1 <> 0)");
    });
    

    并且属性应该可以由 NHibernate 分配

    public virtual bool IsClientSide { get; protected set; }
    

    现在我们可以在任何查询中使用IsClientSide...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多