简体   繁体   English

流利的休眠数据不持久

[英]fluent nhibernate data is not persisting

Here is the helper; 这是帮手;

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

Below code is the code to store data: 下面的代码是存储数据的代码:

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

My problem is when i run this code and i refresh database, i see data is going through. 我的问题是,当我运行此代码并刷新数据库时,我看到数据正在通过。

When i restart the application, data is gone. 当我重新启动应用程序时,数据不见了。

How can i persist the data to db? 如何将数据持久保存到数据库?

It looks as if you are constantly re-creating the database file: 似乎您正在不断地重新创建数据库文件:

SchemaExport(cfg).Create(

As asked and solved here: 如此处所要求和解决的:

How do I configure FluentNHibernate to not overwrite an existing SQLite db file? 如何配置FluentNHibernate不覆盖现有的SQLite数据库文件?

So it is persisting within an app session, but once you restart the app (or more accurately call SchemaExport again) the database file is re-created. 因此它一直存在于应用程序会话中,但是一旦您重新启动应用程序(或更准确地再次调用SchemaExport ),就会重新创建数据库文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM