NHibernate.QueryException: Failed to resolve property - column name with identifier

When I try to execute the class below using 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)); } 

for class

 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; } } 

with display

 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(); } 

It throws an exception,

NHibernate.QueryException: Failed to resolve property: UserGroupID

How to get the property allowed?

+4
source share
1 answer

Instead of specifying column names in your query, try using class property identifiers (lowercase letters at the end):

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

To avoid this problem in the future, I suggest you use the QueryOver API , which provides type checking at compile time:

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

All Articles