本文介绍了FluentNHibernate - ReferencesAny(),如何使用QueryOver与过滤器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UserAccount 类,它可能属于 Employee 或者一个 Client



我不知道如何QueryOver一个对象OwnerRefObject 字段,过滤器。



例如:

  public class UserAccount 
{
public virtual int Id {get; set;}
public virtual string UserName {get; set;}
public virtual string Password {get; set;}
public virtual对象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);




内部服务:

  public UserAccount GetClientUserAccountByClientId(int clientId)
{
var result = _userAccountRepository $ b $ .QueryOver()
.Where (x => x.OwnerRefObject是客户端)
//在这里我想要类似于(x => x.OwnerRefObject.Id == clientId)
.Future()
.FirstOrDefault ();

返回结果;


解决方案

心神。我不能自由地测试其中哪些可以工作。




  • 看看您是否可以使用限制。 IdEq(object)(在 NHibernate.Criterion 命名空间中),所以你不必引用实际的 Id 属性,...

  • ...或者您可以尝试使用客户端和 Employee 实现具有 Id 属性的 IUserOwner 接口。 >


我希望有机会在有更多时间的情况下回过头来细化这个答案。


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;
}
解决方案

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.

这篇关于FluentNHibernate - ReferencesAny(),如何使用QueryOver与过滤器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 02:12