Entity framework 实体框架:单个模型上单个应用程序中的多个数据库提供程序

Entity framework 实体框架:单个模型上单个应用程序中的多个数据库提供程序,entity-framework,sqlite,entity-framework-6,system.data.sqlite,Entity Framework,Sqlite,Entity Framework 6,System.data.sqlite,我目前正在开发一个应用程序,它是围绕EF4连接到MSSQL构建的。 现在已经决定,我们也应该能够连接到SQLite 我已经将应用程序升级到EF6,确保MSSQL的所有功能都能正常工作,现在我正在尝试让应用程序连接到SQLite DB(这是我们的MSSQL DB的转换版本) 可悲的是,我在这里碰到了一堵墙 如果我尝试以与MSSQL相同的方式创建SQLite连接 EntityConnectionStringBuilder entityBuilder = new EntityC

我目前正在开发一个应用程序,它是围绕EF4连接到MSSQL构建的。 现在已经决定,我们也应该能够连接到SQLite

我已经将应用程序升级到EF6,确保MSSQL的所有功能都能正常工作,现在我正在尝试让应用程序连接到SQLite DB(这是我们的MSSQL DB的转换版本)

可悲的是,我在这里碰到了一堵墙

如果我尝试以与MSSQL相同的方式创建SQLite连接

            EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder();
            entityBuilder.Provider = "System.Data.SQLite.EF6";
            entityBuilder.ProviderConnectionString = "data source=sqlite_master.db";
            entityBuilder.Metadata = @"res://*/Model.csdl|res://*/Model.ssdl|res://*/Model.msl";                                
            connection = new EntityConnection(entityBuilder.ToString());
            connection.Open();

            theEntities = new masterEntities(connection); // masterEntities extends System.Data.Entity.DbContext
            getDataBaseVersion();
我得到一个异常,声明在getDataBaseVersion方法中“System.Data.SQLite.SQLiteConnection”不能转换为“System.Data.SqlClient.SqlConnection”(这是第一个在实体上运行查询的方法)

我已经尝试了一些方法,但总是会遇到这种情况或“意外的代码优先异常”

顺便说一句:我真的不需要在运行时为SQLite版本创建连接字符串。如果这个问题可以通过使用配置文件来解决,我很乐意这样做

我的app.config如下所示:

  <entityFramework>
    <providers>
      <provider invariantName="System.Data.SQLite" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
  <system.data>
    <DbProviderFactories>
      <remove invariant="System.Data.SQLite"/>
      <add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".Net Framework Data   Provider for SQLite"
      type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" />
    </DbProviderFactories>
  </system.data>
  <entityFramework>
    <providers>
      <provider invariantName="System.Data.SQLite.EF6" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
  <system.data>
    <DbProviderFactories>
      <remove invariant="System.Data.SQLite.EF6" />
      <add name="SQLite Data Provider (Entity Framework 6)" invariant="System.Data.SQLite.EF6" description=".NET Framework Data Provider for SQLite (Entity Framework 6)" type="System.Data.SQLite.EF6.SQLiteProviderFactory, System.Data.SQLite.EF6" />
    </DbProviderFactories>
  </system.data>
好吧,我现在放弃。 我现在已经创建了一个DB First SQLite模型,当我尝试使用MSSQL提供程序连接到它时,我得到了相同的异常,只是相反

看起来我必须使用plan B并编译两个版本的应用程序。一个用于sqlite,一个用于mssql。

好的,我现在放弃。 我现在已经创建了一个DB First SQLite模型,当我尝试使用MSSQL提供程序连接到它时,我得到了相同的异常,只是相反


看起来我必须使用plan B并编译两个版本的应用程序。一个用于sqlite,一个用于mssql。

我看不出有什么问题,我怀疑引用的程序集或上下文构造函数中有错误。为什么不使用DbContext(Connection,contextOwnsConnection)重载呢?我发现这样做比在app.cfg中定位连接字符串容易得多。谢谢你的回复。我在幕后使用DbContext(Connection,contextOwnsConnection)。被调用的masterEntities的构造函数如下所示:public masterEntities(System.Data.Common.DbConnection connection):base(connection,true){}问题可能出在EntityConnection中(到目前为止必须转换为SQLConnection,现在必须转换为SQLiteConnection)。您是否显式地强制转换这些连接?无论如何,我注意到您读取了System.Data.Sqlite.SqliteProviderServices(没有EF)。这是正确的吗?我真的不知道我自己是怎么处理的,明天上班时我会考虑,不应该引起这个问题,但这是需要注意的。我自己昨天在System.Data.Sqlite.SqliteProviderServices上绊倒了。看起来不正确(请参阅我在主帖子上的更新)。不,我自己没有在任何地方强制转换连接。我看不出有什么问题,我怀疑引用的程序集或上下文构造函数中有错误。为什么不使用DbContext(Connection,contextOwnsConnection)重载呢?我发现这样做比在app.cfg中定位连接字符串容易得多。谢谢你的回复。我在幕后使用DbContext(Connection,contextOwnsConnection)。被调用的masterEntities的构造函数如下所示:public masterEntities(System.Data.Common.DbConnection connection):base(connection,true){}问题可能出在EntityConnection中(到目前为止必须转换为SQLConnection,现在必须转换为SQLiteConnection)。您是否显式地强制转换这些连接?无论如何,我注意到您读取了System.Data.Sqlite.SqliteProviderServices(没有EF)。这是正确的吗?我真的不知道我自己是怎么处理的,明天上班时我会考虑,不应该引起这个问题,但这是需要注意的。我自己昨天在System.Data.Sqlite.SqliteProviderServices上绊倒了。看起来不正确(请参阅我在主帖子上的更新)。不,我自己不在任何地方建立联系。
EntityFramework.dll // from nuget
EntityFramework.BulkInsert // from nuget
EntityFramework.MappingAPI // from nuget
EntityFramework.SqlServer // from nuget
System.Data
System.Data.DataSetExtensions
System.Data.Linq
System.Data.SQLite // from the sqlite-netFx451-setup-bundle-x86
System.Data.SQLite.EF6 // from the sqlite-netFx451-setup-bundle-x86