【问题标题】:Fluent NHibernate automatically deletes dataFluent NHibernate 自动删除数据
【发布时间】:2013-03-02 20:56:26
【问题描述】:

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

是否有任何错误的配置?

【问题讨论】:

    标签: c# asp.net-mvc postgresql fluent-nhibernate


    【解决方案1】:

    我想说问题出在这里:

    .ExposeConfiguration(cfg => new SchemaExport(cfg).Create(true,true))
    

    您似乎每次在构建会话工厂时都在导出架构。现在我不是 100% 确定(我从来没有使用代码优先来生成表,只映射到我事先创建的现有数据库),但我敢打赌,导出会覆盖你已经拥有的(删除和重新创建或其他) .

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

    【讨论】:

    • 既然您说它“从数据库中删除了每条记录”,那么我假设您的数据库已经存在。 ExposeConfiguration 是可选的,您可能不需要它。
    猜你喜欢
    • 2012-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-30
    • 1970-01-01
    相关资源
    最近更新 更多