2012-03-27 70 views
1

這裏是幫手;流利的nhibernate數據不存在

public class NHibernateHelper 
{ 
    private static ISessionFactory _sessionFactory; 

    private static ISessionFactory SessionFactory 
    { 
     get 
     { 
      if (_sessionFactory == null) 
      { 
       InitSessionFactory(); 
       return _sessionFactory; 
      } 
     } 

    private static void InitSessionFactory() 
    { 
     _sessionFactory = 
      Fluently.Configure().Database(
       MsSqlConfiguration.MsSql2008.ConnectionString(
        "ConnectionString").ShowSql). 
       Mappings(m => m.FluentMappings.AddFromAssemblyOf<Category>()).ExposeConfiguration(
        cfg => new SchemaExport(cfg).Create(true, true)).BuildSessionFactory(); 
    } 

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

下面的代碼是代碼存儲數據:

using(var session = NHibernateHelper.OpenSession()) 
{ 
    using (var tx = session.BeginTransaction()) 
    { 
     session.Save(category); 
     tx.Commit(); 
    } 
} 

我的問題是,當我運行這段代碼和我刷新數據庫,我看到數據正在經歷。

當我重新啓動應用程序時,數據消失了。

我該如何堅持數據到數據庫?

+0

導出架構是否重新創建數據庫或只是在需要時更新? – wiero 2012-03-27 08:06:45

回答

1

看起來好像你不斷地重新創建數據庫文件:

SchemaExport(cfg).Create(

至於問及這裏解決:

How do I configure FluentNHibernate to not overwrite an existing SQLite db file?

所以它是一個應用程序會話中持續,但是一旦您重新啓動應用程序(或更準確地再次調用SchemaExport),數據庫文件就會重新創建。

+0

是的,你是對的。謝謝。 – DarthVader 2012-03-27 08:10:34

+0

哈哈。我知道。我也知道如何+1。謝謝。 – DarthVader 2012-03-27 08:18:38

+0

@DarthVader夠公平的,我沒注意你的代表。 – 2012-03-27 08:19:16