【发布时间】:2013-10-24 22:07:12
【问题描述】:
我有一个使用 nHibernate 的小型 POC 应用程序。这是我第一次自己设置 nHibernate,但我之前也使用过它。出于某种原因,我的查询没有返回任何数据。我可以确认我的数据库中有一个Product。
public class NHibernateHelper
{
private static String _connectionString =
@"Server=localhost\SQLEXPRESS;Database=TestProject;User ID=TestProjectMvc;Password=Pa$$word";
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(_connectionString).ShowSql()
).Mappings(m => m.FluentMappings.AddFromAssemblyOf<Product>())
//.ExposeConfiguration(cfg => new SchemaExport(cfg).Create(true, true))
.BuildSessionFactory();
}
public static ISession OpenSession()
{
return SessionFactory.OpenSession();
}
}
我的映射类:
public class ProductMap : ClassMap<Product>
{
public ProductMap()
{
Id(x => x.Id);
Map(x => x.Name);
References(x => x.Category).Column("CategoryId");
}
}
以及我用来检索数据的方法:
public IEnumerable<Product> GetAllProducts()
{
using (var session = NHibernateHelper.OpenSession())
{
var list = session.QueryOver<Product>().List();
return list;
}
}
【问题讨论】:
-
您的映射和域对象是否在同一个程序集中?
-
@SimonWhitehead 啊就是这样...我将我的
Mappings更改为从ProductMap程序集加载,我现在正在查看数据。谢谢!如果你愿意,发表你的答案,我会这样标记。
标签: c# sql-server-2008 nhibernate fluent-nhibernate