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

我的梦境 提交于 2019-12-14 03:09:05

问题


I have an UserAccount class, which may belong to an Employee or a Client

I don't know how to QueryOver a object OwnerRefObject field, with a filter.

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

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

回答1:


Two possible approaches that come to mind. I am not free to test which of these would work at the moment.

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

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



来源:https://stackoverflow.com/questions/21940396/fluentnhibernate-referencesany-how-to-use-queryover-with-filter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!