代码优先实体框架 - 创建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.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() 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 45I 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
最满意答案
有两种水平约束 - 通用和语言约束。
普遍约束称为左和右。 这些约束的含义永远不会改变。
语言约束的约束称为前导和尾随。 它们的含义取决于运行应用程序的语言环境。 如果语言类似于英语,则保留前导,尾随是正确的; 但是如果语言是从右到左书写的阿拉伯语或希伯来语,则前导是正确的,并且留下尾随。 这样就可以构建无论书写方向如何都能正常工作的标签。
这种区别在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:
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.
相关问答
更多-
所以这个答案已经被Andrew Barber删除了,但是它的确很有帮助,所以我再次发布它: 1)不要在界面构建器中列出视图或组件。 2)纯粹以编程方式添加你的视图,从alloc / init开始,并适当地设置它们的框架。 3)完成。 希望有所帮助! PS。 您还可以使用以下方法试验从视图中剥离约束: [查看removeConstraints:view.constraints]但我有更纯粹的代码方法的运气。 So this answer was deleted by Andrew Barber, but it ...
-
如何从viewDidLayoutSubviews调用GC元素的setNeedsDisplay()(或者您正在绘制它的方式) 您可以像这样使用GCD的dispatch_once: var token : dispatch_once_t = 0 override func viewDidLayoutSubviews() { super.viewDidLayoutSubviews() dispatch_once(&token, { () -> Void in cgthing.set ...
-
如果你想使用约束,你可以这样做: var previousAnchor = scrollView.leadingAnchor for _ in 0 ..< 5 { let textView = UITextView() textView.translatesAutoresizingMaskIntoConstraints = false textView.backgroundColor = .green textView.isScrollEnabled = false ...
-
如何为水平图像视图的自动布局正确设置约束?(How to properly set constrains for Auto layout for horizontal image views?)[2022-08-23]
问题是你何时开始 我正在使用自动布局,所以我必须添加约束,我首先要做的是为第一个图像视图添加顶部空间约束(左上角),它告诉我视图不明确。 因此,当您设置顶部和左侧时,您的视图知道x和y,但它仍然不知道它需要为自己设置的宽度和高度。 这是你应该做的下一步。 找出一种方法,以便约束可以确定高度和宽度。 一般示例将给出底部和右侧,固定宽度和高度等等,其可以是n个案例。 设置相等的宽度和高度,然后将图像视图设置为1:1 aspect radio 您已将约束设置为宽度和高度以及某些宽高比。 此时,图像视图仍然不知道宽 ... -
将标签和imageview包装在wrapview中 然后使用自动调整包装视图 view.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin; wrap the label & imageview in a wrapview then autoresize the wrapview using view.autoresizingMask = UIViewAutoresi ...
-
当旋转发生时,您需要以编程方式更新UIStackView的axis属性。 文件说: 确切的布局取决于堆栈视图的轴,分布,对齐,间距和其他属性。 纯粹使用故事板是无法实现这一目标的。 工作解决方案 我创建了一个演示项目并验证它的工作原理。 我由一个视图控制器组成。 class ViewController: UIViewController { @IBOutlet weak var stackView: UIStackView! override func viewWillTransiti ...
-
EmailTextField.center = CGPointMake( self.view.bounds.size.width / 2, your Y); 用这个 EmailTextField.center = CGPointMake( self.view.bounds.size.width / 2, your Y); Use this
-
有两种水平约束 - 通用和语言约束。 普遍约束称为左和右。 这些约束的含义永远不会改变。 语言约束的约束称为前导和尾随。 它们的含义取决于运行应用程序的语言环境。 如果语言类似于英语,则保留前导,尾随是正确的; 但是如果语言是从右到左书写的阿拉伯语或希伯来语,则前导是正确的,并且留下尾随。 这样就可以构建无论书写方向如何都能正常工作的标签。 这种区别在Interface Builder中使用“Respect language direction”菜单项绘制; 要获得通用的左右约束,请取消选中它: 因此,对于 ...
-
如何在iOS中使用自动布局按比例增长的图像视图?(How to make imageviews that proportionally grow with auto layout in iOS?)[2023-11-20]
添加到第一个答案,您最好的选择是在故事板中设置宽高比。 Ctrl +从图像视图拖动到超级视图,就像通常用于添加自动布局约束一样,并选择相等的宽度和/或相等的高度(对于您的项目更有意义)。 此时,您的图像将与整个视图的大小相同。 你可能不希望这样。 在右侧工具栏的尺寸检查器中,找到autolayout约束。 您可以修改乘数以按比例调整图像大小。 如果将其设置为0.2,则图像的高度和/或宽度将为超视图高度和/或宽度的20%。 本教程 (第1部分和第2部分)将帮助您。 对于第二个问题,如果将图像添加到默认的Ass ... -
写在UIViewController的-viewDidLoad方法中: [super viewDidLoad]; // Assuming viewA and viewB have already been setup and have their // translatesAutoresizingMaskIntoConstraints property set to NO NSDictionary *views = NSDictionaryOfVariableBindings(viewA,viewB) ...