- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想创建一个集成测试,它从数据库中获取一个 EF 实体,将其克隆到一个分离的对象,对其进行修改,然后将其保存回来并再次与原始对象进行比较。
但是,我使用 AutoMapper 创建类的克隆,但事实证明这也被跟踪或原始对象的别名。我需要它与 EF 完全分离,并且能够在我的存储库类之外执行此操作(即不使用任何 EF 分离方法)。
这样做的原因是我的 EF 类包含其他类的嵌套集合,而 EF 不处理持久化整个对象树。因此,我的存储库类中的 Update() 方法处理这个问题,我希望我的 NUnit 测试来测试这个代码。我希望测试能够快速创建我的原始类的副本,而无需 EF 跟踪它。
最佳答案
Creating a cloned object containing current, original, or database values The DbPropertyValues object returned from CurrentValues, OriginalValues, or GetDatabaseValues can be used to create a clone of the entity. This clone will contain the property values from the DbPropertyValues object used to create it. For example:
using (var context = new UnicornsContext())
{
var unicorn = context.Unicorns.Find(1);
var clonedUnicorn = context.Entry(unicorn).GetDatabaseValues().ToObject();
}Note that the object returned is not the entity and is not being tracked by the context. The returned object also does not have any relationships set to other objects.
The cloned object can be useful for resolving issues related to concurrent updates to the database, especially where a UI that involves data binding to objects of a certain type is being used. (See Part 9 for more details on dealing with optimistic concurrency.)
希望对他人有所帮助
关于entity-framework - 如何为测试目的创建 EF codefirst 类的分离克隆?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5597270/
我在带有 SQL Server 数据库的 .NET Core 项目中使用代码优先迁移。但是由于自己解封,我的同事不得不手动更改数据库表。有没有一种优雅的方法可以将迁移与数据库同步,并在手动干预后继续使
我正在尝试创建一个具有 Entity Framework 代码优先环境的数据库。数据库中的表是身份表。 问题出在 SQL Server 的数据库创建上。当我运行应用程序时没有创建数据库。 但是,我可以
我尝试使用 SQLite 为 Entity Framwork 运行 CodeFirst 示例。NuGet 包 SQLite.CodeFirst 已安装并运行无误但它不会创建 SQLite 数据库。这是
我遇到“一个IEntityChangeTracker的多个实例无法引用一个实体对象”问题。经过一番检查之后,看来我有一个正在跟踪更改的对象。问题是我不知道问题对象的来源……它显然已经放到上下文中,但是
我为我的应用程序支持的每种数据库类型创建了过程。并添加到他们的迁移文件中。 我可以在我的代码优先应用程序中调用存储过程MSSQL,就像这两种类型一个 worker.StoredProcedures.E
我搜索了很多但都失败了...我正在尝试创建两个表,一个具有另一个的外键(具体来说,Url_Entries 的 url 是 WordCount 类)。但是当我使用代码优先将对象添加到数据库时,我遇到了异
我在 CodeFirst 脚手架 ASP.NET MVC 站点中使用了以下模型(简化): Public Tax { public int ID {get; set; } public st
我正在使用带有 SQLite 数据库的 WPF 应用程序。尝试使用 SQLite.CodeFirst 以代码优先的方式设计数据库。 错误是:{“创建模型时无法使用上下文。如果在OnModelCreat
该表未在数据库中按应命名的方式命名。这是上下文: 应用数据库上下文: public IDbSet Artwork { get; set; } 在数据库中,该表应命名为 Artwork,但它没有命名为
我有一个看起来像这样的类电子邮件: public class Email { [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
我正在使用 ASP.net-MVC 开发一个项目,由于它是模型优先,它会自动生成数据库的 .mdf 和 .ldf 文件。数据库是2014版本的。当我想将其上传到主机服务器上时,他们告诉我他们不支持20
我想将一个属性用作两个独立实体的外键 - 一次作为复合外键的一部分,一次作为单个外键。这是我的意思的一个简单示例: public class ParentEntity{ [Key, Colum
假设您有一个带有订单状态的订单类,我想在 OrderStatus 类中声明 OrderStatusId。但是,默认情况下没有设置外键关系。如果我在列上使用 [ForeignKey] 属性,它似乎需要一
考虑我有以下类(class); 产品类别 鱼制品 非鱼产品 一个 ProductCategory 可以有许多 FishProducts 和/或许多 NonFishProducts,这是一种非常简单的一
好的,所以我已经阅读了其他线程,但我仍然一无所获。我将我的项目设置为启动,我在 App.config 中有一个连接字符串。我在 VS2013 中使用 LocalDb。 如果我删除我的数据库,它会创建它
我在当前的项目中使用 EF Migrations 有一段时间了,一切都很好,直到今天,情况如下: 我做了一个小改动,添加了一个字符串属性 我调用了一个 API 方法并收到错误消息,指出模型发生了变化
这是我的两个模型类学生和类(class) public class Student { private readonly IStudentOps _studentOps; public
我的公司有一个实习生网站项目。我在 Code First 项目中使用 MVC5 和 Entity Framework 6。 这是我公司的内部网,所以我想要两个数据库。1 在生产中(发布)。1 在开发(
我使用 EF CodeFirst 并想使用来自包管理器控制台的命令行在数据库中重新创建表,该怎么做? 问题是在开发 EF CodeFirst 模型时经常更改,我手动删除数据库中的表,然后 EF 在第一
我想创建一个集成测试,它从数据库中获取一个 EF 实体,将其克隆到一个分离的对象,对其进行修改,然后将其保存回来并再次与原始对象进行比较。 但是,我使用 AutoMapper 创建类的克隆,但事实证明
我是一名优秀的程序员,十分优秀!