本文介绍了NHibernate:通用方法中的QueryOver的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个测试方法,我对List方法有问题。我想使用几个类(所有实现IAdminDecimal)。在QueryOver上,我有这个错误:
类型'T'必须是一个引用类型才能在通用类型或方法'NHibernate.ISession.QueryOver()中将其用作参数'T' '
using(var session = sessions.OpenSession())
{
using (var tx = session.BeginTransaction())
{
CurrentSessionContext.Bind(session);
AdministrationService服务=新的AdministrationService(会话);
service.List< AdminDelay>();
tx.Commit();
$ / code $ / pre
$ b $ p
public class AdministrationService
{
private readonly ISession _session;
公共管理服务(ISession会话)
{
_session = session;
}
公共IList< T> List< T>()其中T:IAdminDecimal
{
var res = _session.QueryOver< T>()。List< T>();
return res;
}
}
public interface IAdminDecimal
{
int Id {get;组; }
int Code {get;组; }
十进制值{get;组; }
bool IsDeleted {get;组; }
}
public class AdminVAT:IAdminDecimal
{
public virtual int Id {get;组; }
public virtual int Code {get;组; }
public virtual decimal Value {get;组; }
public virtual bool IsDeleted {get;组; }
}
解决方案 >
public IList< T> List< T>()其中T:class,IAdminDecimal
I have this test method, I have a problem on the "List" method. I'd like use several class (all inplement IAdminDecimal). On the QueryOver, I have this error : The type 'T' must be a reference type in order to use it as parameter 'T' in the generic type or method 'NHibernate.ISession.QueryOver()'
using (var session = sessions.OpenSession())
{
using (var tx = session.BeginTransaction())
{
CurrentSessionContext.Bind(session);
AdministrationService service = new AdministrationService(session);
service.List<AdminDelay>();
tx.Commit();
}
}
The class :
public class AdministrationService
{
private readonly ISession _session;
public AdministrationService(ISession session)
{
_session = session;
}
public IList<T> List<T>() where T : IAdminDecimal
{
var res = _session.QueryOver<T>().List<T>();
return res;
}
}
public interface IAdminDecimal
{
int Id { get; set; }
int Code { get; set; }
decimal Value { get; set; }
bool IsDeleted { get; set; }
}
public class AdminVAT : IAdminDecimal
{
public virtual int Id { get; set; }
public virtual int Code { get; set; }
public virtual decimal Value { get; set; }
public virtual bool IsDeleted { get; set; }
}
解决方案 Try:
public IList<T> List<T>() where T : class, IAdminDecimal
这篇关于NHibernate:通用方法中的QueryOver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!