gwi*_*003 3 c# nhibernate fluent-nhibernate sql-server-2008
我正在使用 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();
}
}
Run Code Online (Sandbox Code Playgroud)
我的映射类:
public class ProductMap : ClassMap<Product>
{
public ProductMap()
{
Id(x => x.Id);
Map(x => x.Name);
References(x => x.Category).Column("CategoryId");
}
}
Run Code Online (Sandbox Code Playgroud)
以及我用来检索数据的方法:
public IEnumerable<Product> GetAllProducts()
{
using (var session = NHibernateHelper.OpenSession())
{
var list = session.QueryOver<Product>().List();
return list;
}
}
Run Code Online (Sandbox Code Playgroud)
从另一种类型的程序集中添加映射的极其有用的方法总是会让其他人犯错。
对于其他想知道的人..问题是这样的:
.AddFromAssemblyOf<Product>()
Run Code Online (Sandbox Code Playgroud)
Product
并ProductMap
参加了不同的集会。因此,在所在的程序集中找不到映射Product
。
我已经看到很多人开始这个旅行了!
归档时间: |
|
查看次数: |
998 次 |
最近记录: |