代码优先实体框架 - 创建 SQL CE DB 时出错

发布于 2024-09-11 18:55:12 字数 3181 浏览 4 评论 0原文

我一直在使用实体框架 CTP 和代码优先,如 本教程,由 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>

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

测试方法 MyProjet.Tests.Administration.ModlelTests.Business.TestGetBusinessesList 抛出异常: System.Reflection.TargetInitationException: 异常已被抛出 调用的目标。 ---> 系统类型初始化异常: 类型初始值设定项 '系统.Data.SqlServerCe.SqlCeProviderServices' 抛出异常。 ---> 系统.安全.验证异常: 操作可能会破坏稳定 运行时。

具有以下堆栈跟踪:

System.Data.SqlServerCe.SqlCeProviderServices..ctor() System.Data.SqlServerCe.SqlCeProviderServices..cctor() System.RuntimeFieldHandle.GetValue(RtFieldInfo 字段、对象实例、运行时类型 字段类型、运行时类型声明类型、 布尔&域初始化) System.Reflection.RtFieldInfo.InternalGetValue(对象 obj, 布尔值 doVisibilityCheck, 布尔值 doCheckConsistency) System.Reflection.RtFieldInfo.InternalGetValue(对象 obj,布尔值 doVisibilityCheck) System.Reflection.RtFieldInfo.GetValue(对象 对象) System.Data.SqlServerCe.ExtensionMethods.SystemDataSqlServerCeSqlCeProviderServices_Instance_GetValue() System.Data.SqlServerCe.ExtensionMethods.SystemDataSqlServerCeSqlCeProviderServices_Instance() System.Data.SqlServerCe.SqlCeProviderFactory.System.IServiceProvider.GetService(类型 服务类型) System.Data.Common.DbProviderServices.GetProviderServices(DbProviderFactory 工厂) System.Data.Common.DbProviderServices.GetProviderServices(DbConnection 联系) System.Data.Entity.ModelConfiguration.Internal.Configuration.CodeFirstCachedMetadataWorkspace.GetMetadataWorkspace(DbConnection 商店连接) System.Data.Entity.Infrastruct.DbModel.CreateObjectContext[TContext](DbConnection 现有连接) System.Data.Entity.Internal.LazyInternalContext.InitializeFromModel(DbModel 模型) System.Data.Entity.Internal.LazyInternalContext.InitializeContext() System.Data.Entity.Internal.InternalContext.Initialize() System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(类型 实体类型) System.Data.Entity.Internal.Linq.EfInternalQuery1.Initialize() System.Data.Entity.Internal.Linq.EfInternalQuery1.Include(String 小路) System.Data.Entity.Infrastruct.DbQuery`1.Include(字符串 小路) MyProjet.Areas.Administration.Models.BusinessModel.GetBusinesses() 在 D:\projects2010\MyProjet\MyProjet\Areas\Administration\Models\BusinessModel.cs: 47号线 MyProjet.Tests.Administration.ModlelTests.Business.TestGetBusinessesList() 在 D:\projects2010\MyProjet\MyProjet.Tests\Administration\ModlelTests\Business.cs: 第 45 行

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

已经为此苦苦挣扎了一段时间,只是无法弄清楚。我确信我忽略了一些简单的事情。

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

っ左 2024-09-18 18:55:13

尝试使用

<代码>
Database.DefaultConnectionFactory = new SqlCeConnectionFactory("System.Data.SqlServerCe.4.0");

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

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/

荒芜了季节 2024-09-18 18:55:13

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文