簡體   English   中英

流暢的NHibernate會自動刪除數據

[英]Fluent NHibernate automatically deletes data

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

有沒有錯誤的配置?

我會說問題出在這里:

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

每次構建會話工廠時,您似乎都在導出模式。 現在我不是100%肯定(我從來沒有使用代碼優先生成表,只映射到我之前創建的現有數據庫),但我打賭導出會覆蓋你已經擁有的東西(刪除和重新創建或其他) 。

使用Fluent NHibernate導出模式時,有人遇到類似的問題,SQLite文件會被覆蓋,所以我要說你必須小心你何時以及如何(重新)創建數據庫:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM