开发者

How to restrict selection with QueryOver in (fluent) nhibernate?

I want to filter objects from the db by a property that comes from another object but i get an exception:

A first chance exce开发者_如何学编程ption of type 'System.Collections.Generic.KeyNotFoundException' occurred in mscorlib.dll

A first chance exception of type 'NHibernate.QueryException' occurred in NHibernate.dll

A first chance exception of type 'NHibernate.QueryException' occurred in NHibernate.dll

The program '[5116] Examples.FirstProject.vshost.exe: Managed (v2.0.50727)' has exited with code -532459699 (0xe0434f4d).

This works:

var curves = session.QueryOver<Curve>().WhereRestrictionOn(p => p.Name).IsLike("%CurveName%").List();
foreach (Curve curve in curves)
{
    Console.WriteLine(" ID:\t{0}\n Name:\t{1}\n Group:\t{2}\n", curve.Id, curve.Name, curve.Group.Name);
}

This not, it outputs the exception information:

var curves = session.QueryOver<Curve>().WhereRestrictionOn(p => p.Group.Name).IsLike("%GroupName%").List();
foreach (Curve curve in curves)
{
    Console.WriteLine(" ID:\t{0}\n Name:\t{1}\n Group:\t{2}\n", curve.Id, curve.Name, curve.Group.Name);
}

These are my mappings:

public class CurveMap : ClassMap<Curve>
{
    public CurveMap()
    {
        Table("CURVES");
        Id(x => x.Id).Column("CURVE_ID");
        Map(x => x.Name).Column("NAME");
        References(x => x.Group).Column("GROUP_ID");
    }
}

public class CurveGroupMap : ClassMap<CurveGroup>
{
    public CurveGroupMap()
    {
        Table("GROUPS");
        Id(x => x.Id).Column("GROUP_ID");
        Map(x => x.Name).Column("NAME");
        HasMany(x => x.Curves).KeyColumn("GROUP_ID").Cascade.All().Inverse();
    }
}

And these are my objects

public class Curve
{
    public virtual int Id { get; private set; }
    public virtual string Name { get; set; }
    public virtual CurveGroup Group { get; set; }
}

public class CurveGroup
{
    public virtual int Id { get; private set; }
    public virtual string Name { get; set; }
    public virtual IList<Curve> Curves { get; set; }
}

Any idea, how to fix this. I am new to (fluent) nhibernate.


If you join CurveGroup and use Aliases it will work:

CurveGroup cgAlias = null;
var curves = session.QueryOver<Curve>()
    .JoinAlias(e => e.Group, () => cgAlias)
    .WhereRestrictionOn(() => cgAlias.Name).IsLike("%GroupName%").List();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜