gpt4 book ai didi

entity-framework - 代码优先 Entity Framework - 创建 SQL CE DB 时出错

转载 作者:行者123 更新时间:2023-12-04 08:31:29 27 4
gpt4 key购买 nike

我一直在使用 Entity Framework CTP 和 Code-First,如 this tutorial Scott Guthrie 和 Scott Hanselman 的另一篇文章(无法发布链接,但谷歌搜索“Simple Code First with Entity Framework 4 - Magic Unicorn Feature CTP 4”)。这对于主要的 MVC 应用程序来说工作得很好,但我现在正在尝试添加一个测试项目,它使用一个单独的 SQL CE 数据库。

我已将以下内容添加到 App.Config 文件中:

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

但是,当我尝试运行测试时,它会在尝试创建数据库时抛出以下错误:

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.

使用以下堆栈跟踪:

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.EfInternalQuery
1.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

我已尝试替换 MVC 应用程序中现有的 MyData 连接字符串,它工作正常。仅当将其添加到测试项目时才会导致此问题。此外,当指向 SQL 或 SQL Express 数据库时,测试项目可以正常工作。

一直在纠结这个问题,就是想不通。我确定我忽略了一些简单的事情。

最佳答案

尝试使用


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

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

关于entity-framework - 代码优先 Entity Framework - 创建 SQL CE DB 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3344641/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com