C# Fluent NHibernate自动删除数据

C# Fluent NHibernate自动删除数据,c#,asp.net-mvc,postgresql,fluent-nhibernate,C#,Asp.net Mvc,Postgresql,Fluent Nhibernate,我在ASP.NET MVC4应用程序中使用PostgreSQL和Fluent NHibernate,并使用代码优先实体和映射 当我运行应用程序时,它会自动从数据库中删除所有记录 这是我的NHibernateHelper课程 public class NHibernateHelper { private static ISessionFactory _sessionFactory; private static ISessionFactory SessionFactory

我在ASP.NET MVC4应用程序中使用PostgreSQL和Fluent NHibernate,并使用代码优先实体和映射

当我运行应用程序时,它会自动从数据库中删除所有记录

这是我的NHibernateHelper课程

public class NHibernateHelper
{
    private static ISessionFactory _sessionFactory;

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

    private static void InitializeSessionFactory()
    {
        _sessionFactory = Fluently.Configure()
            .Database(PostgreSQLConfiguration.Standard
                        .ConnectionString(
                            @"Server=localhost;Port=5432;Database=TestDB;User=postgres;Password=postgre;")
                        .ShowSql()
            )
            .Mappings(m => m.FluentMappings.AddFromAssemblyOf<ProductCategory>())
            .ExposeConfiguration(cfg => new SchemaExport(cfg)
                            .Create(true, true))
            .BuildSessionFactory();
    }

    public static ISession OpenSession() 
    {
        return SessionFactory.OpenSession();
    }
公共类NHibernateHelper
{
私人静态ISessionFactory_sessionFactory;
私有静态ISessionFactory会话工厂
{
得到
{
if(_sessionFactory==null)
初始化SessionFactory();
返回工厂;
}
}
私有静态void InitializeSessionFactory()
{
_sessionFactory=fluntly.Configure()
.Database(PostgreSQLConfiguration.Standard
.连接字符串(
@“服务器=localhost;端口=5432;数据库=TestDB;用户=postgres;密码=postgre;”)
.ShowSql()
)
.Mappings(m=>m.FluentMappings.AddFromAssemblyOf())
.ExposeConfiguration(cfg=>newschemaexport(cfg)
.创建(true,true))
.BuildSessionFactory();
}
公共静态会话OpenSession()
{
返回SessionFactory.OpenSession();
}

是否存在任何不正确的配置?

我认为问题在于:

.ExposeConfiguration(cfg => new SchemaExport(cfg).Create(true,true))
似乎每次构建会话工厂时都要导出架构。现在我不是100%确定(我从来没有先使用代码生成表,只是映射到我之前创建的现有数据库),但我打赌导出会覆盖您已经拥有的内容(删除并重新创建或其他)

有人在使用Fluent NHibernate导出模式时遇到了类似的SQLite文件被覆盖的问题,因此我想说,您必须小心何时以及如何(重新)创建数据库:)

,因为您说过它“从数据库中删除了所有记录”然后我假设您的数据库已经存在。ExposeConfiguration是可选的,您可能不需要它。