我无法弄清楚这个基本的 NHibernate 查询我做错了什么
好的,我有一个类,Company
public class Company
{
public virtual int Id { get; set; }
public virtual IList<Role> Roles { get; set; }
}
和另一个类,Role
public class Role
{
public virtual int Id { get; set; }
public virtual Company Company { get; set; }
public virtual RoleLevel RoleLevel { get; set; }
}
我正在使用 Fluent 自动映射,没有什么特别的事情发生。
我有这个方法:
private RoleLevel GetRole(int companyId)
{
var allRoles = Session.CreateCriteria<Role>().List<Role>();
var role = Session.CreateCriteria<Role>()
.CreateAlias(NameOf<Role>.Property(r => r.Company), "c")
.Add(Restrictions.Eq("c.Id", companyId))
.UniqueResult<Role>();
return (role == null) ? RoleLevel.Restricted : role.RoleLevel;
}
companyId
作为 102 传入。如果我检查 allRoles
数组,其中之一与 Company #102 相关。但是应该返回单个 Role
的查询返回 null
。
我做错了什么?
编辑 1
根据请求,根据 NProf 的说法,这里是正在执行的查询:
SELECT this_.Id as Id75_1_,
this_.Version as Version75_1_,
this_.RoleLevel as RoleLevel75_1_,
this_.DbDate as DbDate75_1_,
this_.Account_id as Account5_75_1_,
this_.Company_id as Company6_75_1_,
c1_.Id as Id71_0_,
c1_.Version as Version71_0_,
c1_.Name as Name71_0_,
c1_.OnyxAlias as OnyxAlias71_0_,
c1_.DbDate as DbDate71_0_,
c1_.Parent_Id as Parent6_71_0_
FROM "Role" this_
inner join "Company" c1_
on this_.Company_id = c1_.Id
WHERE c1_.Id = 102 /* @p0 */
编辑 2
我尝试将数据库更改为 SQL Server。在 SQL Server 中,这一切都有效。我猜这是 NHibernate 连接 SQLite 数据库的一个错误?目前,我可以使用 SQL Server 数据库进行测试,但出于速度原因,我希望将来使用内存 SQLite 数据库。
Okay, I have a class, Company
public class Company
{
public virtual int Id { get; set; }
public virtual IList<Role> Roles { get; set; }
}
And another class, Role
public class Role
{
public virtual int Id { get; set; }
public virtual Company Company { get; set; }
public virtual RoleLevel RoleLevel { get; set; }
}
I'm using the Fluent automappings, nothing special going on.
I have this method:
private RoleLevel GetRole(int companyId)
{
var allRoles = Session.CreateCriteria<Role>().List<Role>();
var role = Session.CreateCriteria<Role>()
.CreateAlias(NameOf<Role>.Property(r => r.Company), "c")
.Add(Restrictions.Eq("c.Id", companyId))
.UniqueResult<Role>();
return (role == null) ? RoleLevel.Restricted : role.RoleLevel;
}
companyId
is being passed in as 102. If I check the allRoles
array, one of them relates to Company #102. But the query that is supposed to return a single Role
returns null
.
What am I doing wrong?
EDIT 1
By request, here is the query being executed, according to NHProf:
SELECT this_.Id as Id75_1_,
this_.Version as Version75_1_,
this_.RoleLevel as RoleLevel75_1_,
this_.DbDate as DbDate75_1_,
this_.Account_id as Account5_75_1_,
this_.Company_id as Company6_75_1_,
c1_.Id as Id71_0_,
c1_.Version as Version71_0_,
c1_.Name as Name71_0_,
c1_.OnyxAlias as OnyxAlias71_0_,
c1_.DbDate as DbDate71_0_,
c1_.Parent_Id as Parent6_71_0_
FROM "Role" this_
inner join "Company" c1_
on this_.Company_id = c1_.Id
WHERE c1_.Id = 102 /* @p0 */
EDIT 2
I tried changing the database to SQL Server. In SQL Server, it all works. I guess this is a bug with how NHibernate connects to SQLite databases? For now, I can use a SQL Server DB for testing, but I'd like to go for an In Memory SQLite DB in the future, for speed reasons.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,您是否对数据库运行了查询?
其次,如果映射出现问题,您可以尝试另一个 CreateCriteria 吗?然后您可以指定连接类型,如下所示。只是为了确保我们在进一步操作之前已经尝试了所有基础知识:)
第三,潜在的问题可能是您的表名周围有双引号。这表明存在映射问题。表名大小写不匹配或类似情况。
First of all have you run the query against the database?
Second, if something is wrong with the mapping, could you try another CreateCriteria instead? You can then speficfy the jointype like below. Just to make sure we have tried all the basics before we move further :)
Third, potential problem could be that you have double quotes around your table names. That indicates a mapping problem. Casing of table name mismatch or something similar.