如何在(流畅的)nhibernate 中使用 QueryOver 限制选择?

发布于 2024-10-31 10:09:41 字数 1989 浏览 4 评论 0原文

我想通过来自另一个对象的属性来过滤数据库中的对象,但出现异常:

mscorlib.dll 中发生了“System.Collections.Generic.KeyNotFoundException”类型的第一次机会异常
NHibernate.dll 中第一次出现“NHibernate.QueryException”类型异常
NHibernate.dll 中第一次出现“NHibernate.QueryException”类型异常
程序“[5116] Examples.FirstProject.vshost.exe:托管 (v2.0.50727)”已退出,代码为 -532459699 (0xe0434f4d)。

这有效:

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);
}

这不行,它输出异常信息:

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);
}

这些是我的映射:

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();
    }
}

这些是我的对象

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; }
}

任何想法,如何解决这个问题。我是(流利的)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 exception 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.

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

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

发布评论

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

评论(1

爱给你人给你 2024-11-07 10:09:41

如果您加入 CurveGroup 并使用别名,它将起作用:

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

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