如何在(流畅的)nhibernate 中使用 QueryOver 限制选择?
我想通过来自另一个对象的属性来过滤数据库中的对象,但出现异常:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您加入 CurveGroup 并使用别名,它将起作用:
If you join CurveGroup and use Aliases it will work: