2015-05-12 58 views
1

今天,我們正在開發針對SQL Server CE和Entity Framework 6.13作爲ORM(代碼優先)的Windows環境的解決方案。然而,我們正在研究將它移植到Linux環境的可用性,當然,由於Linux不支持SQL Server數據庫,我們打算在Linux機器上使用SQLite,並在Windows機器上繼續使用SQL Server。對於這兩種環境,解決方案(.sln)都是相同的。具有多個數據庫服務器的實體框架

因此,有可能有一個實體框架模型連接多個數據庫(SQLServer,SQLite,..)?我應該爲每個數據庫創建一個模型嗎?

對於這種情況,NHibernate是比Entity Framework更好的解決方案嗎?或者還有其他什麼?

我已經找到了一些有關這方面的答案,但最近沒有人。 Ps:代碼第一是沒有必要的,如果需要,我們打開它來改變它。

非常感謝! :-)使用NHibernate(帶FluentNHibernate)和代碼

回答

1

它會像

using NHCfg = NHibernate.Cfg; 

var config = new Configuration().AddMappingsFromAssembly<Entity>(); 
if (UseSqlCe) 
{ 
    config.SetProperty(NHCfg.Environment.Driver, typeof(SqlCeDriver).AssemblyQualifiedName); 
    config.SetProperty(NHCfg.Environment.Dialect, typeof(SqlCeDialect).AssemblyQualifiedName); 
} 
else if (UseSqlite) 
{ 
    config.SetProperty(NHCfg.Environment.Driver, typeof(Sqlite20Driver).AssemblyQualifiedName); 
    config.SetProperty(NHCfg.Environment.Dialect, typeof(SqliteDialect).AssemblyQualifiedName); 
} 

隨着EF,你需要對一些緊密聯繫MSSQLSERVER(CE),如打這將是類似於http://rob.conery.io/2014/02/05/using-entity-framework-6-with-postgresql/

  • 遷移或代碼優先創建?對於MSSQL工作
  • 默認模式是「DBO」

我的建議是與您更熟悉什麼開始。

提醒你,我有偏見,但是有些原因的NHibernate:

  • 在內存中簡單的測試使用SQLite和

    config.SetProperty(NHibernate.Cfg.Environment.ConnectionProvider, typeof(SingletonConnectionProvider).AssemblyQualifiedName); 
    
    /// <summary> 
    /// ensures that the same connection is used for all sessions. Useful for in-memory databases like sqlite 
    /// </summary> 
    public class SingletonConnectionProvider : DriverConnectionProvider 
    { 
        private IDbConnection _theConnection; 
    
        public override void CloseConnection(IDbConnection conn) 
        { 
        } 
    
        public override IDbConnection GetConnection() 
        { 
         if (_theConnection == null) 
         { 
          _theConnection = base.GetConnection(); 
         } 
         return _theConnection; 
        } 
    } 
    
  • schemacreation爲bioth數據庫(SchemaExport工具類)

  • 讀使用期貨的配料