I have a C# .Net 4.0 console application for testing purposes (using VS 2012). My aim is to be able to create a single Entity Framework .edmx file that can be used on both an MS SQL Server database, and an SQLite database. Basically, I want to use the same entity model classes and collections for querying, but easily be able to switch between the two different databases at will.
我有一个用于测试目的的C#.Net 4.0控制台应用程序(使用VS 2012)。我的目标是能够创建一个可以在MS SQL Server数据库和SQLite数据库上使用的单个Entity Framework .edmx文件。基本上,我想使用相同的实体模型类和集合进行查询,但很容易就能随意在两个不同的数据库之间切换。
So far I have created my .edmx file by connection to MS Server database and adding my single test table (called Contact). With this I can then use the following code to get data from my table:
到目前为止,我已经通过连接到MS Server数据库并添加了我的单个测试表(称为Contact)来创建我的.edmx文件。有了这个,我可以使用以下代码从我的表中获取数据:
var db = new DataAccess.ContactTestEntities();
foreach (var contact in db.Contacts)
Console.WriteLine("" + contact.ID + ". " + contact.FirstName + " " + contact.LastName);
Now, I want to be able to use the same code but connect to an SQLite database instead. I have written a partial class that allows my to change the connection string on contruction like so:
现在,我希望能够使用相同的代码,而是连接到SQLite数据库。我编写了一个部分类,允许我更改contruction上的连接字符串,如下所示:
var db = new DataAccess.ContactTestEntities("MY SQLITE CONNECTION STRING");
It works fine in that respect except when trying to query the database I get this error:
它在这方面工作正常,除了在尝试查询数据库时我收到此错误:
Unable to cast object of type 'System.Data.SQLite.SQLiteConnection' to type 'System.Data.SqlClient.SqlConnection'.
无法将类型为“System.Data.SQLite.SQLiteConnection”的对象强制转换为“System.Data.SqlClient.SqlConnection”。
I have tried to find a solution to this but have hit a dead end and I am struggling to find the next step to take.
我试图找到解决方案,但已经走到了尽头,我正在努力寻找下一步。
So that is my question: How can I get past this problem? Or is there another approach I can take to get the same desired results?
这就是我的问题:我怎样才能解决这个问题?或者我可以采取另一种方法来获得相同的预期结果?
Stack Trace for above exception:
堆栈跟踪以上异常:
at System.Data.SqlClient.SqlCommand.set_DbConnection(DbConnection value) at System.Data.Common.Utils.CommandHelper.SetStoreProviderCommandState(EntityCommand entityCommand, EntityTransaction entityTransaction, DbCommand storeProviderCommand) at System.Data.EntityClient.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior) at System.Data.Objects.Internal.ObjectQueryExecutionPlan.Execute[TResultType](ObjectContext context, ObjectParameterCollection parameterValues) at System.Data.Objects.ObjectQuery
1.GetResults(Nullable
1 forMergeOption) at System.Data.Objects.ObjectQuery1.System.Collections.Generic.IEnumerable<T>.GetEnumerator() at System.Data.Entity.Internal.Linq.InternalQuery
1.GetEnumerator()
at System.Data.Entity.Internal.Linq.InternalSet1.GetEnumerator()
1.System.Collections.Generic.IEnumerable.GetEnumerator() at SQLiteTest.Program.ReadFromSqlite() in c:\Development\Projects\Test Applications\SQLiteTest\SQLiteTest\Program.cs:line 82 at SQLiteTest.Program.ReadTests() in c:\Development\Projects\Test Applications\SQLiteTest\SQLiteTest\Program.cs:line 63 at SQLiteTest.Program.ProcessMenu() in c:\Development\Projects\Test Applications\SQLiteTest\SQLiteTest\Program.cs:line 36 at SQLiteTest.Program.Main(String[] args) in c:\Development\Projects\Test Applications\SQLiteTest\SQLiteTest\Program.cs:line 14 at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Data.Entity.Infrastructure.DbQuery
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart()处于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, SystemB上的System.Data.Objects.Internal.ObjectQueryExecutionPlan.Execute [TResultType](ObjectContext context,ObjectParameterCollection parameterValues)中的System.Data.Objects.1.EetRetults(Nullable1 forMergeOption)处于System.Data.Objects.ObjectQuery1.System的CommandBehavior行为。 System.Data.Entity上的System.Data.Entity.Internal.Linq.InternalSet1.GetEnumerator()中System.Data.Entity.Internal.Linq.InternalQuery1.GetEnumerator()的Collections.Generic.IEnumerable
.GetEnumerator() .Inbase的基础设施.DbQuery1.System.Collections.Generic.IEnumerable.GetEnumerator(),位于c:\ Development \ Projects \ Test Applications \ SQ中的SQLiteTest.Program.ReadFromSqlite() LiteTest \ SQLiteTest \ Program.cs:位于c:\ Development \ Projects \ Test Applications \ SQLiteTest \ SQLiteTest \ Program.cs中的SQLiteTest.Program.ReadTests()中的第82行:位于c中的SQLiteTest.Program.ProcessMenu()中的第63行: \ Development \ Projects \ Test Applications \ SQLiteTest \ SQLiteTest \ Program.cs:第13行,位于c:\ Development \ Projects \ Test Applications \ SQLiteTest \ SQLiteTest \ Program.cs中的SQLiteTest.Program.Main(String [] args):line 14位于System.TereadingStudio上的System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly,String [] args),System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext,ContextCallback callback,Object state,Boolean preserveSyncCtx)中的Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()在System.Threading.ThreadHelper.Thre的System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback回调,对象状态)中的System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback回调,Object状态,Boolean preserveSyncCtx) adStart()
1 个解决方案
#1
0
Are you passing in the connection string or the name of the connection string in the <connectionStrings>
app settings section? I believe the issue is as you describe. It defaults the provider to System.Data.SqlClient. If you want Sql Lite, you have to set the providerName on the <connectionString>
and then send the name (attribute) of that <connectionString>
to DbContext (It knows how to look that up automatically). It should then use that new providerName attribute instead of SqlClient.
您是否在
#1
0
Are you passing in the connection string or the name of the connection string in the <connectionStrings>
app settings section? I believe the issue is as you describe. It defaults the provider to System.Data.SqlClient. If you want Sql Lite, you have to set the providerName on the <connectionString>
and then send the name (attribute) of that <connectionString>
to DbContext (It knows how to look that up automatically). It should then use that new providerName attribute instead of SqlClient.
您是否在