NHibernate : QueryOver in generic method

2020-04-14 03:35发布

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

1条回答
倾城 Initia
2楼-- · 2020-04-14 04:11

Try:

public IList<T> List<T>() where T : class, IAdminDecimal
查看更多
登录 后发表回答