2012-03-27 71 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