【问题标题】:Fluent nHibernate not returning dataFluent nHibernate 不返回数据
【发布时间】: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


【解决方案1】:

从另一种类型的程序集中添加映射的非常有用的方法总是会绊倒其他人。

对于其他想知道的人......问题是这样的:

.AddFromAssemblyOf<Product>()

ProductProductMap 在不同的程序集中。因此在 Product 所在的程序集中找不到映射。

我已经看到很多人开始了这次旅行!

【讨论】:

  • 很好的解释!哈哈,大多数人不会花这么多精力写一个他们已经知道会被标记为答案的答案。赞一个!
【解决方案2】:

另外 - 如果您使用 SchemaExport 为您生成数据库表 - /do/ 请记住在您第二次运行应用程序时将其关闭。否则对你来说又是一张空白的桌子……

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-25
    • 1970-01-01
    • 1970-01-01
    • 2010-11-27
    相关资源
    最近更新 更多