NHibernate.QueryException:无法解析属性 - 具有ID的列名

时间:2012-06-25 22:52:12

标签: c# nhibernate exception

当我尝试使用ICriteria执行以下类时,

if (_userGroupId > 0 && _userId > 0 )
{
   return session.CreateCriteria(typeof(UserUserGroup))
             .Add(Restrictions.Eq("UserGroupID", _userGroupId))
               .Add(Restrictions.Eq("UserID", _userId))
               .Add(Restrictions.Eq("Deleted", false));
}

为班级

public class UserUserGroup
{
    public virtual long UserUserGroupId { get; set; }
    public virtual long UserGroupId { get; set; }
    public virtual long UserId { get; set; }
    public virtual bool Deleted { get; set; }

    public UserUserGroup() {}

    public UserUserGroup(long userGroupId, long userId)
    {
        UserGroupId = userGroupId;
        UserId = userId;
    }

}

使用Mapping,

public void Override(AutoMapping<UserUserGroup> mapping)
    {
        mapping.Id(map => map.UserUserGroupId, "UserUserGroupID").GeneratedBy.HiLo("hibernate_unique_key", "next_hi", "100", "tablename='UserUserGroups'");
        mapping.Map(map => map.UserId,"UserID").Nullable();
        mapping.Map(map => map.UserGroupId,"UserGroupID").Nullable();
        mapping.Map(map => map.Deleted,"Deleted").Nullable();
    }

抛出异常,

  

NHibernate.QueryException:无法解析属性:UserGroupID

如何解决该属性?

1 个答案:

答案 0 :(得分:6)

不要在查询中指定列名,而是尝试使用类的属性标识符(末尾用小写字母):

return session.CreateCriteria(typeof(UserUserGroup))
              .Add(Restrictions.Eq("UserGroupId", _userGroupId))
              .Add(Restrictions.Eq("UserId", _userId))
              .Add(Restrictions.Eq("Deleted", false))
              .List();

为了避免将来出现这种问题,我建议您使用QueryOver API,它在编译时提供类型检查:

return session.QueryOver<UserUserGroup>()
              .Where(x => x.UserGroupId == _userGroupId)
              .And(x => x.UserId == _userId)
              .And(x => x.Deleted == false)
              .List();