FluentNHibernate - ReferencesAny (), How to use QueryOver with a filter?

I have a UserAccount class that can belong to Employee or Client

I do not know how in the field QueryOver a object OwnerRefObject with a filter.

Example:

 public class UserAccount { public virtual int Id {get;set;} public virtual string UserName {get;set;} public virtual string Password {get;set;} public virtual object OwnerRefObject {get;set;} } public class UserMap:ClassMap<User> { public UserMap() { Id(x => x.Id).GeneratedBy.Indentity(); Map(x => x.UserName); Map(x => x.Password); ReferencesAny(x => x.OwnerRefObject) .IdentityType<int>() .EntityTypeColumn("OwnerObject_Type") .EntityIdentifierColumn("OwnerObject_Id") .AddMetaValue<Client>(typeof(Client).Name) .AddMetaValue<Employee>(typeof(Employee).Name); } } 

Internal service:

 public UserAccount GetClientUserAccountByClientId(int clientId) { var result = _userAccountRepository .QueryOver() .Where(x => x.OwnerRefObject is Client) // Here I want something like (x => x.OwnerRefObject.Id==clientId) .Future() .FirstOrDefault(); return result; } 
0
c # orm nhibernate fluent-nhibernate
source share
1 answer

Two possible approaches that come to mind. I can not check which of them will work at the moment.

  • See if you can use Restrictions.IdEq(object) (in the NHibernate.Criterion namespace) so you don't have to refer to the actual Id property, ...
  • ... or you can try Client and Employee implement the IUserOwner interface, which has the Id property.

I hope I have a chance to come back and clarify this answer when I have more time.

+1
source share

All Articles