简体   繁体   English

我无法弄清楚这个基本的NHibernate查询在做什么错

[英]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.

相关问题 无法弄清楚我做错了什么 ASP.NET C# - Can't figure out what I'm doing wrong ASP.NET C# Fluent NHibernate没有自动映射和架构创建功能:我不知道怎么了 - No automapping and schema creation with Fluent NHibernate: I can't figure out what's wrong 我在这个实现中做错了什么? - What I'm doing wrong with this implementation? Nhibernate,痛苦缓慢的查询,我做错了吗? - Nhibernate, painfully slow query, am I doing it wrong? 需要帮助找出我在if语句中做错了什么 - Need help figuring out what I'm doing wrong in my if statement 无法使数据绑定正常工作(我读了很多帖子,也弄不清楚我在做什么错) - Can not get Databinding to work (I Have read many posts and can not figure out what I am doing wrong) DataContract运行时错误-类型&#39;myType&#39;无法序列化。 我做错了什么? - DataContract runtime error - Type 'myType' cannot be serialized. What i'm doing wrong? 这个查询我在做什么错? - What am I doing wrong with this query? 我继承了一个较旧的Web窗体站点代码,无法弄清楚它出了什么问题 - I've inherited an older web forms site code and cannot figure out what is wrong with it 使用布尔标志无法脱离任务-我在做什么错? - Cannot break out of a Task with the use of boolean flags - what am I doing wrong?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM