同时使用entity framework与SQL Server和SQLite数据库

我有一个用于测试目的的C#.Net 4.0控制台应用程序(使用VS 2012)。 我的目标是能够创建一个可以在MS SQL Server数据库和SQLite数据库上使用的单个Entity Framework .edmx文件。 基本上,我想使用相同的实体模型类和集合进行查询,但很容易就能随意在两个不同的数据库之间切换。

到目前为止,我已经通过连接到MS Server数据库并添加了我的单个测试表(称为Contact)来创建我的.edmx文件。 有了这个,我可以使用以下代码从我的表中获取数据:

var db = new DataAccess.ContactTestEntities(); foreach (var contact in db.Contacts) Console.WriteLine("" + contact.ID + ". " + contact.FirstName + " " + contact.LastName); 

现在,我希望能够使用相同的代码,而是连接到SQLite数据库。 我编写了一个部分类,允许我更改contruction上的连接字符串,如下所示:

 var db = new DataAccess.ContactTestEntities("MY SQLITE CONNECTION STRING"); 

它在这方面工作正常,除了在尝试查询数据库时我收到此错误:

无法将类型为“System.Data.SQLite.SQLiteConnection”的对象强制转换为“System.Data.SqlClient.SqlConnection”。

我试图找到解决方案,但已经走到了尽头,我正在努力寻找下一步。

这就是我的问题 :我怎样才能解决这个问题? 或者我可以采取另一种方法来获得相同的预期结果?


堆栈跟踪以上exception:

处于System.Data.Ents.Clmand.AntityClient.EntityCommandDefinition.ExecuteStoreCommands的System.Data.Common.Utils.CommandHelper.SetStoreProviderCommandState(EntityCommand entityCommand,EntityTransaction entityTransaction,DbCommand storeProviderCommand)中的System.Data.SqlClient.SqlCommand.set_DbConnection(DbConnection值)(EntityCommand entityCommand, System.Data.Objects.Object上的System.Data.Objects.Internal.ObjectQueryExecutionPlan.Execute [TResultType](ObjectContext context, 1.GetResults(Nullable parameterValues)中的CommandBehavior行为)System.Data.Objects.ObjectQuery上的1.GetResults(Nullable 1 forMergeOption) 1.System.Collections.Generic.IEnumerable.GetEnumerator() at System.Data.Entity.Internal.Linq.InternalQuery 1.GetEnumerator()
在System.Data.Entity.Internal.Linq.InternalSet 1.GetEnumerator()
at System.Data.Entity.Infrastructure.DbQuery
1.GetEnumerator()
at System.Data.Entity.Infrastructure.DbQuery
1.GetEnumerator()
at System.Data.Entity.Infrastructure.DbQuery
1.System.Collections.Generic.IEnumerable.GetEnumerator()at SQLiteTest.Program.ReadFromSqlite()in c:\ Development \ Projects \ Test Applications \ SQLiteTest \ SQLiteTest \ Program.cs:在C:\ Development \ Projects \ Test Applications \ SQLiteTest \ SQLiteTest \ Program.cs中的SQLiteTest.Program.ReadTests()中的第82行:位于c:\ Development \ Projects \ Test Applications \中的SQLiteTest.Program.ProcessMenu()中的第63行SQLiteTest \ SQLiteTest \ Program.cs:位于C:\ Development \ Projects \ Test Applications \ SQLiteTest \ SQLiteTest \ Program.cs中的SQLiteTest.Program.Main(String [] args)的第36行:System.AppDomain._nExecuteAssembly中的第14行( Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()中的RuntimeAssembly程序集,String [] args)
System.Threading.ExecutionContext.Run上的System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext,ContextCallback回调,Object状态,Boolean preserveSyncCtx)在System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback回调,Object状态,Boolean preserveSyncCtx)处于System.Threading.ExecutionContext.Run System.Threading.ThreadHelper.ThreadStart()中的(ExecutionContext executionContext,ContextCallback回调,对象状态)

您是否在应用程序设置部分中传递连接字符串或连接字符串的名称? 我相信问题就像你描述的那样。 它将提供程序默认为System.Data.SqlClient。 如果你想要Sql Lite,你必须在上设置providerName,然后将该的名称(属性)发送给DbContext(它知道如何自动查找)。 然后它应该使用新的providerName属性而不是SqlClient。