3

I have been using Entity Framework CTP with Code-First as in this tutorial by Scott Guthrie and another by Scott Hanselman (can't post the link, but google "Simple Code First with Entity Framework 4 - Magic Unicorn Feature CTP 4"). This is working perfectly for the main MVC application, but I am now trying to add a testing project, that uses a separate SQL CE Database.

I have added the following to the App.Config file:

<connectionStrings>
    <add name="MyData"
        connectionString="Data Source=D:\myProject\myDb.sdf;"
        providerName="System.Data.SqlServerCe.4.0" />
</connectionStrings>

However when I try to run the tests it throws the following error when trying to create the database:

Test method MyProjet.Tests.Administration.ModlelTests.Business.TestGetBusinessesList threw exception: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.TypeInitializationException: The type initializer for 'System.Data.SqlServerCe.SqlCeProviderServices' threw an exception. ---> System.Security.VerificationException: Operation could destabilize the runtime.

With the following stack trace:

System.Data.SqlServerCe.SqlCeProviderServices..ctor() System.Data.SqlServerCe.SqlCeProviderServices..cctor() System.RuntimeFieldHandle.GetValue(RtFieldInfo field, Object instance, RuntimeType fieldType, RuntimeType declaringType, Boolean& domainInitialized) System.Reflection.RtFieldInfo.InternalGetValue(Object obj, Boolean doVisibilityCheck, Boolean doCheckConsistency) System.Reflection.RtFieldInfo.InternalGetValue(Object obj, Boolean doVisibilityCheck) System.Reflection.RtFieldInfo.GetValue(Object obj) System.Data.SqlServerCe.ExtensionMethods.SystemDataSqlServerCeSqlCeProviderServices_Instance_GetValue() System.Data.SqlServerCe.ExtensionMethods.SystemDataSqlServerCeSqlCeProviderServices_Instance() System.Data.SqlServerCe.SqlCeProviderFactory.System.IServiceProvider.GetService(Type serviceType) System.Data.Common.DbProviderServices.GetProviderServices(DbProviderFactory factory) System.Data.Common.DbProviderServices.GetProviderServices(DbConnection connection) System.Data.Entity.ModelConfiguration.Internal.Configuration.CodeFirstCachedMetadataWorkspace.GetMetadataWorkspace(DbConnection storeConnection) System.Data.Entity.Infrastructure.DbModel.CreateObjectContext[TContext](DbConnection existingConnection) System.Data.Entity.Internal.LazyInternalContext.InitializeFromModel(DbModel model) System.Data.Entity.Internal.LazyInternalContext.InitializeContext() System.Data.Entity.Internal.InternalContext.Initialize() System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) System.Data.Entity.Internal.Linq.EfInternalQuery1.Initialize() System.Data.Entity.Internal.Linq.EfInternalQuery1.Include(String path) System.Data.Entity.Infrastructure.DbQuery`1.Include(String path) MyProjet.Areas.Administration.Models.BusinessModel.GetBusinesses() in D:\projects2010\MyProjet\MyProjet\Areas\Administration\Models\BusinessModel.cs: line 47 MyProjet.Tests.Administration.ModlelTests.Business.TestGetBusinessesList() in D:\projects2010\MyProjet\MyProjet.Tests\Administration\ModlelTests\Business.cs: line 45

I have tried replacing the existing MyData connection string in the MVC application, and it works fine. It only causes this problem when this is added to the Testing project. Additionally the testing project works without problem when pointed at an SQL or SQL Express Database.

Have been struggling with this for a while now, and just can't figure it out. I am sure I have overlooked something simple.

4

2 回答 2

4

尝试使用

Database.DefaultConnectionFactory = new SqlCeConnectionFactory("System.Data.SqlServerCe.4.0");

请参阅我的博客文章以获取示例http://www.arrangeactassert.com/code-first-entity-framework-unit-test-examples/

于 2010-07-28T10:11:06.413 回答
0

我一直在内置 Microsoft 测试框架下运行测试。将测试框架更改为 NUnit(如 Jag 的教程中所示)已解决了该问题。

所以看起来 SqlServerCe 和 Visual Studio单元测试框架之间存在冲突。

于 2010-07-30T10:57:30.840 回答