Nhibernate session.CreateCriteria始终返回零记录
我有一个基本方法,它不从数据库中获取任何记录。在配置中,它创建表时没有问题。它总是返回零记录。我在下面添加了配置文件。我不明白是什么问题Nhibernate session.CreateCriteria始终返回零记录,nhibernate,Nhibernate,我有一个基本方法,它不从数据库中获取任何记录。在配置中,它创建表时没有问题。它总是返回零记录。我在下面添加了配置文件。我不明白是什么问题 public List<Note> GetAll() { var session = NHibernateHelper.OpenSession(); var note = session.CreateCriteria<Note>().List<Note>();
public List<Note> GetAll()
{
var session = NHibernateHelper.OpenSession();
var note = session.CreateCriteria<Note>().List<Note>();
return note.ToList();
}
public List GetAll()
{
var session=NHibernateHelper.OpenSession();
var note=session.CreateCriteria().List();
return note.ToList();
}
助手:
public class NHibernateHelper
{
private static ISessionFactory _sessionFactory;
private static ISessionFactory SessionFactory
{
get
{
if (_sessionFactory == null)
{
InitializeSessionFactory();
}
return _sessionFactory;
}
}
private static void InitializeSessionFactory()
{
_sessionFactory = Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2008
.ConnectionString(
@"Server={server};Database=ToDo;
User ID=lorem;Password=lorem;
Trusted_Connection=False;Encrypt=True;Connection Timeout=30;")
.ShowSql()
)
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<Note>())
.ExposeConfiguration(cfg => new SchemaExport(cfg)
.Create(true, true))
.BuildSessionFactory();
}
public static ISession OpenSession()
{
return SessionFactory.OpenSession();
}
}
公共类NHibernateHelper
{
私人静态ISessionFactory_sessionFactory;
私有静态ISessionFactory会话工厂
{
收到
{
if(_sessionFactory==null)
{
初始化SessionFactory();
}
返回工厂;
}
}
私有静态void InitializeSessionFactory()
{
_sessionFactory=fluntly.Configure()
.数据库(MsSqlConfiguration.MsSql2008
.连接字符串(
@“服务器={Server};数据库=ToDo;
用户ID=lorem;密码=lorem;
Trusted_Connection=False;Encrypt=True;连接超时=30;“”)
.ShowSql()
)
.Mappings(m=>m.FluentMappings.AddFromAssemblyOf())
.ExposeConfiguration(cfg=>newschemaexport(cfg)
.创建(true,true))
.BuildSessionFactory();
}
公共静态会话OpenSession()
{
返回SessionFactory.OpenSession();
}
}
试试这个:
我真的不知道它是怎么做的。我使用流利的nhibernate,我无法提供正确的信息如何实现这一点。但问题是我的n层结构。所以,当我在同一层中获得domain、map和nhibernate助手时,就没有问题了。否则它将无法工作。您可以使用Fluent执行以下操作以激活日志:)