3

私は、ScottGuthrieによるこのチュートリアルとScottHanselmanによる別のチュートリアルのようにCode-FirstでEntityFramework CTPを使用しています(リンクを投稿することはできませんが、Google "Simple Code First with Entity Framework 4-Magic Unicorn Feature CTP 4")。これはメインのMVCアプリケーションで完全に機能していますが、別のSQLCEデータベースを使用するテストプロジェクトを追加しようとしています。

App.Configファイルに以下を追加しました。

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

ただし、テストを実行しようとすると、データベースを作成しようとすると次のエラーがスローされます。

テストメソッドMyProjet.Tests.Administration.ModlelTests.Business.TestGetBusinessesListが例外をスローしました:System.Reflection.TargetInvocationException:呼び出しのターゲットによって例外がスローされました。---> System.TypeInitializationException:「System.Data.SqlServerCe.SqlCeProviderServices」の型初期化子が例外をスローしました。---> System.Security.VerificationException:操作によってランタイムが不安定になる可能性があります。

次のスタックトレースを使用します。

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.SystemDataSqlServerCeSqlCeProvider 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 originalConnection)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.EfInternalQueryGetProviderServices(DbProviderFactoryファクトリ)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 originalConnection)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.EfInternalQueryGetProviderServices(DbProviderFactoryファクトリ)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 originalConnection)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.EfInternalQueryModelConfiguration.Internal.Configuration.CodeFirstCachedMetadataWorkspace.GetMetadataWorkspace(DbConnection storeConnection)System.Data.Entity.Infrastructure.DbModel.CreateObjectContext [TContext](DbConnection previousConnection)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.EfInternalQueryModelConfiguration.Internal.Configuration.CodeFirstCachedMetadataWorkspace.GetMetadataWorkspace(DbConnection storeConnection)System.Data.Entity.Infrastructure.DbModel.CreateObjectContext [TContext](DbConnection previousConnection)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.EfInternalQueryInternal.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.EfInternalQueryInternal.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:行47 MyProjet.Tests.Administration.ModlelTests.Business.TestGetBusinessesList()in D:\ projects2010 \ MyProjet \ MyProjet.Tests \ Administration \ ModlelTests \ Business.cs:行45

MVCアプリケーションで既存のMyData接続文字列を置き換えてみましたが、正常に機能します。これがテストプロジェクトに追加された場合にのみ、この問題が発生します。さらに、SQLまたはSQL Expressデータベースを指定すると、テストプロジェクトは問題なく機能します。

しばらくの間これに苦労していて、それを理解することができません。簡単なことを見落としていると思います。

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 UnitTestingFrameworkの間に競合があるようです

于 2010-07-30T10:57:30.840 に答える