[英]I cannot figure out what I'm doing wrong with this basic NHibernate query
Okay, I have a class, Company
好,我上课,
Company
public class Company
{
public virtual int Id { get; set; }
public virtual IList<Role> Roles { get; set; }
}
And another class, Role
还有另一类,
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. 我正在使用Fluent自动映射,没有什么特别的事情。
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. companyId
被传递为102。如果我检查allRoles
数组,其中之一与公司#102有关。 But the query that is supposed to return a single Role
returns null
. 但是应该返回一个
Role
的查询返回null
。
What am I doing wrong? 我究竟做错了什么?
EDIT 1 编辑1
By request, here is the query being executed, according to NHProf: 根据请求,根据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 编辑2
I tried changing the database to SQL Server. 我尝试将数据库更改为SQL Server。 In SQL Server, it all works.
在SQL Server中,一切正常。 I guess this is a bug with how NHibernate connects to SQLite databases?
我猜这是NHibernate如何连接到SQLite数据库的错误吗? 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.
目前,我可以使用SQL Server数据库进行测试,但是出于速度方面的考虑,我希望将来使用In Memory SQLite数据库。
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? 其次,如果映射有问题,您可以尝试使用另一个CreateCriteria吗? You can then speficfy the jointype like below.
然后,您可以如下指定连接类型。 Just to make sure we have tried all the basics before we move further :)
只是为了确保我们在尝试进一步操作之前已经尝试了所有基础知识:)
private RoleLevel GetRole(int companyId)
{
var allRoles = Session.CreateCriteria<Role>().List<Role>();
var role = Session.CreateCriteria<Role>("r")
.CreateCriteria("Company", "c", JoinType.LeftOuterJoin)
.Add(Restrictions.Eq("c.Id", companyId))
.UniqueResult<Role>();
return (role == null) ? RoleLevel.Restricted : role.RoleLevel;
}
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.
表名称的大小写不匹配或类似的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.