【问题标题】:Entity Framework with mutliple database servers具有多个数据库服务器的实体框架
【发布时间】:2015-05-12 18:56:12
【问题描述】:

今天,我们正在为 Windows 环境开发一个解决方案,其中 SQL Server CE 和 Entity Framework 6.13 作为 ORM(代码优先)。但是,我们正在研究将其移植到 Linux 环境的可用性,当然,由于 Linux 不支持 SQL Server 数据库,我们打算在 Linux 机器上使用 SQLite,并继续在 Windows 机器上使用 SQL Server。两种环境的解决方案 (.sln) 都是相同的。

那么,是否可以让实体框架模型连接多个数据库(SQLServer、SQLite、..)?我应该为每个数据库创建一个模型吗?

对于这种情况,NHibernate 是不是比实体框架更好的解决方案?或者还有什么?

我已经找到了几个答案,但最近没有。

Ps:代码优先不是必需的,如果需要,我们愿意更改它。

非常感谢! :-)

【问题讨论】:

    标签: c# entity-framework nhibernate multi-database


    【解决方案1】:

    使用 NHibernate(使用 FluentNHibernate)并编写代码

    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,它类似于 http://rob.conery.io/2014/02/05/using-entity-framework-6-with-postgresql/,您需要与 MSSqlServer (CE) 建立一些紧密联系,例如:

    • 迁移或代码优先创建?仅?为 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;
          }
      }
      
    • bioth 数据库的架构(SchemaExport 类)

    • 使用期货读取批处理

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-10
      • 2013-03-23
      • 1970-01-01
      • 1970-01-01
      • 2011-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多