NHibernate 查询未返回任何结果

发布于 2024-09-16 23:57:00 字数 2229 浏览 2 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

诗笺 2024-09-23 23:57:00

您没有将映射添加到配置中。

.BuildSessionFactory() 之前添加以下内容:

.Mappings(m => m.FluentMappings.AddFromAssemblyOf<BrandMap>())

You are not adding the mappings to the configuration.

Add this before .BuildSessionFactory():

.Mappings(m => m.FluentMappings.AddFromAssemblyOf<BrandMap>())
无悔心 2024-09-23 23:57:00

我需要对我的代码进行一些修改。正如迭戈所指出的,第一个也是主要的是将映射添加到配置中。

您没有将映射添加到配置中。

在.BuildSessionFactory()之前添加此内容:

.Mappings(m => m.FluentMappings.AddFromAssemblyOf())

我必须修复的下一件事是我的 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.

You are not adding the mappings to the configuration.

Add this before .BuildSessionFactory():

.Mappings(m => m.FluentMappings.AddFromAssemblyOf<BrandMap>())

The next thing I had to fix was my Brand model. I needed to make the Id field virtual.

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 code Table("Brands");

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文