NHibernate 查询未返回任何结果
我正在尝试使用 ASP.NET MVC 正确配置 Fluent 和 NHibernate。据我所知,它配置正确,但是当我访问使用此设置的页面时,我没有收到任何数据结果。
我使用的模型称为 Brand
,数据库表为 Brands
。
这是我的 BrandController 中的一个片段:
public ActionResult Index()
{
IRepository<Brand> repo = new BrandRepository();
var brands = repo.GetAll().ToList<Brand>();
return View(brands);
}
这是我的 BrandRepository 中的一个片段:
ICollection<Brand> IRepository<Brand>.GetAll()
{
using (ISession session = NHibernateHelper.OpenSession())
{
var brands = session
.CreateCriteria(typeof(Brand))
.List<Brand>();
return brands;
}
}
这是我的 NHibernateHelper: 这
public class NHibernateHelper
{
private static ISessionFactory _sessionFactory;
private static ISessionFactory SessionFactory
{
get
{
if (_sessionFactory == null)
{
_sessionFactory = Fluently.Configure()
.Database(
MsSqlConfiguration.MsSql2008
.ConnectionString(c => c
.FromConnectionStringWithKey("ShoesFullAccess")
)
)
.BuildSessionFactory();
}
return _sessionFactory;
}
}
public static ISession OpenSession()
{
return SessionFactory.OpenSession();
}
}
是我的品牌模型:
public class Brand
{
public int Id { get; private set; }
public virtual string Name { get; set; }
public virtual IList<Style> Styles { get; private set; }
public Brand()
{
Styles = new List<Style>();
}
public virtual void AddStyle(Style style)
{
Styles.Add(style);
}
}
最后,这是我的 BrandMap:
public class BrandMap : ClassMap<Brand>
{
public BrandMap()
{
Id(x => x.Id);
Map(x => x.Name);
HasMany(x => x.Styles)
.Inverse()
.Cascade.All();
}
}
如果有人能指出我如何缩小范围的正确方向这个问题我就非常感谢了!
I'm trying to get Fluent and NHibernate configured properly with ASP.NET MVC. As far as I know it's configured properly but when I access the page that uses this setup I am not receiving any data results.
The model I'm using is called Brand
and the database table is Brands
.
Here is a snippet from my BrandController:
public ActionResult Index()
{
IRepository<Brand> repo = new BrandRepository();
var brands = repo.GetAll().ToList<Brand>();
return View(brands);
}
Here is a snippet from my BrandRepository:
ICollection<Brand> IRepository<Brand>.GetAll()
{
using (ISession session = NHibernateHelper.OpenSession())
{
var brands = session
.CreateCriteria(typeof(Brand))
.List<Brand>();
return brands;
}
}
Here is my NHibernateHelper:
public class NHibernateHelper
{
private static ISessionFactory _sessionFactory;
private static ISessionFactory SessionFactory
{
get
{
if (_sessionFactory == null)
{
_sessionFactory = Fluently.Configure()
.Database(
MsSqlConfiguration.MsSql2008
.ConnectionString(c => c
.FromConnectionStringWithKey("ShoesFullAccess")
)
)
.BuildSessionFactory();
}
return _sessionFactory;
}
}
public static ISession OpenSession()
{
return SessionFactory.OpenSession();
}
}
Here is my Brand model:
public class Brand
{
public int Id { get; private set; }
public virtual string Name { get; set; }
public virtual IList<Style> Styles { get; private set; }
public Brand()
{
Styles = new List<Style>();
}
public virtual void AddStyle(Style style)
{
Styles.Add(style);
}
}
And finally, here is my BrandMap:
public class BrandMap : ClassMap<Brand>
{
public BrandMap()
{
Id(x => x.Id);
Map(x => x.Name);
HasMany(x => x.Styles)
.Inverse()
.Cascade.All();
}
}
If anyone could point me in the right direction for how I can narrow down the problem I would very grateful!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您没有将映射添加到配置中。
在
.BuildSessionFactory()
之前添加以下内容:You are not adding the mappings to the configuration.
Add this before
.BuildSessionFactory()
:我需要对我的代码进行一些修改。正如迭戈所指出的,第一个也是主要的是将映射添加到配置中。
我必须修复的下一件事是我的
Brand
模型。我需要将Id
字段设为虚拟
。最后,我需要稍微更改一下我的
BrandMap
—— 我必须通过添加此代码Table("Brands");
I needed to make a few modifications to my code. The first and main one was adding the mappings to the configuration as Diego pointed out.
The next thing I had to fix was my
Brand
model. I needed to make theId
fieldvirtual
.And finally, I needed to change my
BrandMap
just a little bit -- I had to specify exactly which table it was pointing to by adding this codeTable("Brands");