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.

Giles Smith
  • 1,952
  • 15
  • 17
  • `System.Security.VerificationException: Operation could destabilize the runtime.` should mostly never happen. This might be a bug in the provider. – Craig Stuntz Jul 27 '10 at 14:45
  • Hi Craig, Thanks for your answer - I suppose I will just have to keep an eye out for updates. I will post if I find a solution in the mean time. – Giles Smith Jul 28 '10 at 10:02
  • 1
    I'd suggest filing a connect report for the bug, even if @Jag's code works. – Craig Stuntz Jul 28 '10 at 13:02

2 Answers2

4

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
  • 56
  • 1
  • 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 Smith Jul 29 '10 at 10:59
  • 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 Smith Jul 30 '10 at 10:38
  • I added the line to Global.asax.cs in the Application_Start and then everything worked. I was having an issue where it couldn't find the instance. – kamranicus Nov 18 '10 at 21:30
  • I'm not seeing this as a fix for MbUnit - still hitting a similar issue -- http://social.msdn.microsoft.com/Forums/en-US/sqlce/thread/525a838c-acf8-4af9-9b97-7f31b3c90ff8 – Colin Bowern Jan 19 '11 at 20:53
0

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 Smith
  • 1,952
  • 15
  • 17