2009-11-08 57 views
0

我想將我的數據層從Linq2Sql轉換爲nHibernate。我認爲Xml在nHibernate中的配置是非常落後的,所以我使用Fluent。流利的nHibernate不斷重新創建我的數據庫,爲什麼?

我已經設法得到流利,添加一個存儲庫模式和工作單元模式,我的單元測試看起來不錯。

但是現在當我將它插入到服務層時,我注意到每次運行我的應用程序時數據庫都會被重新創建。

我猜這是我的SessionProvider代碼,我不確定我使用的所有擴展。有人可以闡明如何阻止這種情況發生?

public sealed class SessionProvider 
{ 
    private static ISessionFactory _sessionFactory; 

    private static ISessionFactory CreateSessionFactory() 
    { 
     try 
     { 
      return Fluently.Configure() 
       .Database(MsSqlConfiguration.MsSql2005 
           .ConnectionString(Properties.Settings.Default.DBConnection) 
           .Cache(c => c 
               .UseQueryCache() 
               .ProviderClass<HashtableCacheProvider>()) 
           //.ProxyFactoryFactory("NHibernate.ByteCode.Castle.ProxyFactoryFactory,NHiber nate.ByteCode.Castle") 
           .ShowSql()) 
           .Mappings(m=>m.FluentMappings.AddFromAssembly(Assembly.GetExecutingAssembly())) 
       .ExposeConfiguration(BuildSchema) 
       .BuildSessionFactory(); 
     } 
     catch (Exception ex) 
     { 
      System.Diagnostics.Debug.WriteLine(ex.Message); 
      return null; 
     } 


    } 

    public static ISessionFactory SessionFactory 
    { 
     get 
     { 
      if (_sessionFactory == null) 
      { 
       _sessionFactory = CreateSessionFactory(); 
      } 
      return _sessionFactory; 
     } 
    } 

    public static ISession GetSession() 
    { 
     return SessionFactory.OpenSession(); 
    } 

    private static void BuildSchema(Configuration config) 
    { 
     // this NHibernate tool takes a configuration (with mapping info in) 
     // and exports a database schema from it 
     new SchemaExport(config).Create(false, true); 
    } 
} 

回答

3

刪除此行

.ExposeConfiguration(BuildSchema) 

瞭解更多關於新的SchemaExport(配置).Create(假,真); here

其實最後一個參數都是要創建數據庫的。

+0

謝謝邁克。並感謝鏈接也。 – Jordan 2009-11-09 20:32:09

相關問題