Fluent Nhibernate - HQL选择问题
我是 NHibernate 的新手。 我正在使用 Fluent Nhibernate,当我运行一个简单的查询时,我得到空结果。 这是我的配置:
流畅配置:
Fluently.Configure().Database(MsSqlConfiguration.MsSql2008
.ShowSql()
.ConnectionString(c => c
.FromConnectionStringWithKey("MY_DB")))
.Mappings(m =>m.FluentMappings.AddFromAssemblyOf<CsrDAL.Mappings.CsrRegistrationMap>())
.BuildSessionFactory();
实体类
public class Industry
{
public virtual int Id { get; private set; }
public virtual string Name { get; set; }
public virtual IList<CsrRegistration> ReferencedRegistrations { get; set; }
}
映射类
public class IndustryMap : ClassMap<Industry>
{
public IndustryMap()
{
Table("industries");
Id(x => x.Id. "id");
Map(x => x.Name, "name");
}
}
存储库获取方法
public static IList<Industry> getData(CsrRegistration registration)
{
using (var session = CsrDalHelper.OpenSession())
{
using (var transaction = session.BeginTransaction())
{
try
{
IQuery q = session.CreateQuery("from Industry ind where ind.Id=1");
IList<Industry> inds =
q.List<Industry>();
return inds; // <-- This is null
}
}
}
}
提前感谢您! 季米特里斯
I am new to NHibernate.
I am using Fluent Nhibernate and when I run a simple query I get null results.
Here is my configuration:
Fluent Configuration:
Fluently.Configure().Database(MsSqlConfiguration.MsSql2008
.ShowSql()
.ConnectionString(c => c
.FromConnectionStringWithKey("MY_DB")))
.Mappings(m =>m.FluentMappings.AddFromAssemblyOf<CsrDAL.Mappings.CsrRegistrationMap>())
.BuildSessionFactory();
Entity Class
public class Industry
{
public virtual int Id { get; private set; }
public virtual string Name { get; set; }
public virtual IList<CsrRegistration> ReferencedRegistrations { get; set; }
}
Map Class
public class IndustryMap : ClassMap<Industry>
{
public IndustryMap()
{
Table("industries");
Id(x => x.Id. "id");
Map(x => x.Name, "name");
}
}
Repository Fetch Method
public static IList<Industry> getData(CsrRegistration registration)
{
using (var session = CsrDalHelper.OpenSession())
{
using (var transaction = session.BeginTransaction())
{
try
{
IQuery q = session.CreateQuery("from Industry ind where ind.Id=1");
IList<Industry> inds =
q.List<Industry>();
return inds; // <-- This is null
}
}
}
}
Thank you in advance!
Dimitris
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题出在会话管理中,在我的代码的其他地方。
我错误地将集合项添加到 NH 会话之外的实体。
作为 nhibernate 的新手,我现在正在更详细地研究文档。
谢谢
The problem was in session management, somewhere else in my code.
I was wrongly adding collection items to an entity outside the NH session.
As a newcomer to nhibernate, I am now studying the docs in more details.
Thank you