读完NHibernate后数据库变空了

时间:2015-02-04 19:06:44

标签: c# asp.net database nhibernate

我遇到了这个问题,每次我尝试从我的数据库中获取对象时,它都会变空,所有表都保持不变,但是为空,没有记录。我假设它与我的会话在每次操作后被取消有关。我该如何解决这个问题?

以下是相关代码:

以下是我连接数据库的方法,并在每次阅读时使用它。

        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(_connection_string)
            .ShowSql())
            .Mappings(m => m.FluentMappings.AddFromAssemblyOf<House>())
            .ExposeConfiguration(cfg => new SchemaExport(cfg).Create(false, true))
            .BuildSessionFactory();
    }

    public static ISession OpenSession() {
        return SessionFactory.OpenSession();
    }

所以在我写下数据库之后,调用方法来读取,就像这样,DB突然倒空了:

        // Fetch single house
    public static House GetHouse(int house_code) {
        using (var session = NHibernateHelper.OpenSession()) {
                var house = session.QueryOver<House>()
                        .Where(h => h.Code == house_code).List().First();
                return house;
            }
    }

为什么会变空?

0 个答案:

没有答案