使用Effort(EF测试工具)与计算列 [英] Using Effort (EF Testing Tool) with Computed Column
问题描述
我有一些集成测试,直接访问数据库 - 创建测试前提对象 - 执行测试,然后清理 - 然而我赢得了在内存中尝试相同的方法。
I have a number of integration tests which access the DB directly - create test prerequisite objects - performs the tests and then cleans up afterwards - however I wonted to try out the same approach in-memory.
我刚刚在我的项目中使用了 努力 它工作非常容易。但是,我遇到了一个我一直在尝试的问题,但无法解决。
I have just used Effort in my project and it works very easily. However I've hit a problem that I have been trying - but unable to solve.
我需要填充虚拟数据的表之一 - 作为测试前提条件 - 包含计算列(nvarchar,not null)。对于测试的范围,我真的不在乎该列的值 - 但即使我尝试插入虚拟数据,我的数据被忽略,然后我被击中一个错误:
One of the tables that I need filled up with dummy data - as a test prerequisite - contains a computed column (nvarchar, not null). For the scope of the test I really don't care about that column's value - but even if I try to insert dummy data, my data is ignored and then I get hit with an error:
"Column 'x' cannot be null. Error code: GenericError"
在我的测试中,我使用与实际代码使用相同的edmx文件。这阻止我不断更新edmx副本。
In my tests I am using the same edmx file as is used by the actual code. This prevents me from constantly updating the edmx copy.
有一种方法,我可以强制测试更新edmx(在运行时),使列是可空的非计算列? [覆盖OnModelCreating]或有办法插入一个默认值(任何内容为此列)停止此错误? [覆盖SaveChanges]
Is there a way in which I can force the test to update the edmx (at runtime) so that column is a nullable non-computed column? [overriding OnModelCreating] or is there way to insert a default value (anything goes for this column) to stop this error? [overriding SaveChanges]
我目前尝试过以下操作:
I have currently tried the following:
- 对象使用.Attach()而不是.Add()
- 在添加 后将EntityState设置为Unchanged
- 通过Entry.OriginalValues值自实体处于已添加状态]
- Attaching the objects using .Attach() instead of .Add()
- Setting the EntityState to Unchanged after adding
- Forcing the value through Entry.OriginalValues [this values since entity is in Added state]
编辑:
我尝试覆盖了 OnModelCreating 方法,但是没有效果,因为这是DB-First。
I have tried overriding the OnModelCreating method, but to no avail since this is DB-First.
modelBuilder.Entity<Entity_Name>().Property(p => p.x).IsOptional().HasDatabaseGeneratedOption(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.None);
推荐答案
在XML编辑器中打开您的EDMX文件,在StorageModels下
并添加到列定义 StoreGeneratedPattern =Computed
。
Open your EDMX file in XML editor, find your entity under the StorageModels,
and add to the column definition StoreGeneratedPattern="Computed"
.
但如果您更新或删除并添加该表,您将放弃此修改。实际上,你可以编写控制台应用程序,它会更新你的edmx文件,并添加 StoreGeneratedPattern =Computed
在需要的地方,你可以添加这些应用程序到prebuild事件在studio。
But if you update or delete and add that table you will loose this modification. Actually you can write console app that will update you edmx file and add StoreGeneratedPattern="Computed"
where needed and you can add those app to prebuild events in studio.
这篇关于使用Effort(EF测试工具)与计算列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!