首页 \ 问答 \ 代码优先实体框架 - 创建SQL CE DB时出错(Code-First Entity Framework - error creating SQL CE DB)

代码优先实体框架 - 创建SQL CE DB时出错(Code-First Entity Framework - error creating SQL CE DB)

我一直在使用Entity Framework CTP和Code-First,如Scott Guthrie的这个教程和Scott Hanselman的另一个(不能发布链接,但google“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.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.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)连接)System.Data.Entity.ModelCo nfiguration.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.EfInternalQuery 1.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( )在D:\ projects2010 \ MyProjet \ MyProjet \ Areas \ Administration \ Models \ BusinessModel.cs:第47行:D:\ projects2010 \ MyProjet \ MyProjet.Tests中的MyProjet.Tests.Administration.ModlelTests.Business.TestGetBusinessesList() \ Administration \ ModlelTests \ Business.cs:第45行

我试过替换MVC应用程序中现有的MyData连接字符串,它工作正常。 当它添加到Testing项目时,它只会导致此问题。 此外,当指向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.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.


原文:https://stackoverflow.com/questions/3344641
更新时间:2023-06-21 06:06

最满意答案

有两种水平约束 - 通用和语言约束。

  • 普遍约束称为左和右。 这些约束的含义永远不会改变。

  • 语言约束的约束称为前导和尾随。 它们的含义取决于运行应用程序的语言环境。 如果语言类似于英语,则保留前导,尾随是正确的; 但是如果语言是从右到左书写的阿拉伯语或希伯来语,则前导是正确的,并且留下尾随。 这样就可以构建无论书写方向如何都能正常工作的标签。

这种区别在Interface Builder中使用“Respect language direction”菜单项绘制; 要获得通用的左右约束,请取消选中它:

在此处输入图像描述

因此,对于某些视图,您可能正确且正确地使用“前导”和“尾随”,但是从您的投诉中听起来就好像您不小心使用“前导”和“尾随”来表示您要使用“左”和“右”的某些视图。


There are two kinds of horizontal constraint — universal and language-bound.

  • The universal constraints are called Left and Right. The meanings of these constraints never change.

  • The language-bound constraints are called Leading and Trailing. Their meaning depends on the language environment in which the app is running. If the language is like English, Leading is left and Trailing is right; but if the language is like Arabic or Hebrew, which are written from right to left, Leading is right and Trailing is left. This allows labels to be constructed that work correctly regardless of writing direction.

This distinction is drawn in Interface Builder with the "Respect language direction" menu item; to get the universal Left-Right constraints, uncheck it:

enter image description here

So, you may be using Leading and Trailing correctly and appropriately for some views, but it sounds from your complaint as if you have accidentally used Leading and Trailing for some views where you meant to use Left and Right.

相关问答

更多

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。