【问题标题】:fluent nhibernate data is not persisting流利的休眠数据没有持久化
【发布时间】:2012-03-27 08:02:11
【问题描述】:

这是助手;

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();
    }
}

我的问题是当我运行此代码并刷新数据库时,我看到数据正在通过。

当我重新启动应用程序时,数据消失了。

如何将数据持久化到数据库?

【问题讨论】:

  • 导出模式是重新创建数据库还是仅在需要时更新?

标签: c# hibernate fluent-nhibernate


【解决方案1】:

看起来好像您在不断地重新创建数据库文件:

SchemaExport(cfg).Create(

在这里提出并解决:

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

所以它在应用会话中持续存在,但是一旦您重新启动应用(或更准确地说,再次调用 SchemaExport),数据库文件就会重新创建。

【讨论】:

  • @DarthVader 很公平,我没注意你的代表。
猜你喜欢
  • 2012-01-09
  • 1970-01-01
  • 2010-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多