NHibernate: QueryOver nel metodo generico
-
28-10-2019 - |
Domanda
Ho questo metodo di prova, ho un problema con il metodo "List".Vorrei utilizzare diverse classi (tutte inplement IAdminDecimal).Su QueryOver, ho questo errore: Il tipo "T" deve essere un tipo di riferimento per poterlo utilizzare come parametro "T" nel tipo o metodo generico "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();
}
}
La classe:
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; }
}
Soluzione
Prova:
public IList<T> List<T>() where T : class, IAdminDecimal
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow