views:

250

answers:

2

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.

+2  A: 

Try using

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

See my blog post for an example http://www.arrangeactassert.com/code-first-entity-framework-unit-test-examples/

Jag Reehal
Hi Jag, Thanks for the advice, I had tried using SqlCeConnectionFactory("System.Data.SqlServerCe.4.0"); but I might not have been using it in the correct way. I will have a look at your tutorial today and see if I can get it working.
Giles
I have had a look at your tutorial - it is great, will have to go through the whole thing later. Unfortunately I'm still getting the exception. I have tested your code in a number of other projects and it works perfectly - just in my unit testing project. I am using the microsoft testing framework rather than NUnit - I will check to see if perhaps that is causing a conflict. Will post if I find the answer.Thanks again - the tutorial is great.
Giles
A: 

I have been running the tests under the Built in Microsoft testing framework. Changing the test framework to NUnit (as in Jag's tutorial) has fixed the problem.

So looks like there is a conflict between SqlServerCe and the Visual Studio Unit Testing Framework.

Giles