1

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

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

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

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

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

非常感谢!:-)

4

1 回答 1

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 类)

  • 使用期货读取批处理
于 2015-05-18T14:39:42.800 回答