【发布时间】:2018-03-14 22:51:43
【问题描述】:
我有一个包含许多子实体的巨大表单,因此,我使用 Automapper 用树填充 EF 对象。然后我想更新数据库中的类似实体。有什么方法可以将其附加到上下文中?
我尝试这样做:
// ApplicationDriver has many child objects
Infrastructure.Asset.ApplicationDriver driver = mapper.Map<Infrastructure.Asset.ApplicationDriver>(model);
// get similar object from DB
Infrastructure.Asset.ApplicationDriver currentApplication = db.ApplicationDrivers.Where(p => p.ApplicationId == model.ApplicationId).FirstOrDefault();
if (currentApplication == null)
{
db.ApplicationDrivers.Add(driver);
}
else
{
// try to attach driver to current context.
// I want to 'replace' current object with all child objects in DB
db.ApplicationDrivers.Attach(driver);
currentApplication = driver;
}
await db.SaveChangesAsync();
我收到一个错误:
“附加类型为'Infrastructure.Asset.ApplicationDriver'的实体 失败,因为相同类型的另一个实体已经具有相同的 主键值。使用“附加”方法或 将实体的状态设置为“未更改”或“已修改”(如果有) 图中的实体具有冲突的键值。这可能是因为 一些实体是新实体,尚未收到数据库生成的密钥 价值观。在这种情况下,使用“添加”方法或“添加”实体状态 跟踪图,然后将非新实体的状态设置为 “未更改”或“已修改”(视情况而定)。”
我试试:
var currentApplication = db.ApplicationDrivers.Where(p => p.ApplicationId == model.ApplicationId).FirstOrDefault();
if (currentApplication == null)
{
db.ApplicationDrivers.Add(driver);
}
else
{
driver.Id = currentApplication.Id;
db.ApplicationDrivers.Attach(driver);
}
await db.SaveChangesAsync();
并得到同样的错误。什么是不正确的以及如何在不手动将所有子对象的每个属性从驱动程序复制到 currentApplication 的情况下解决此问题?
【问题讨论】:
-
@IvanStoev。非常奇怪的错误:将 datetime2 数据类型转换为 datetime 数据类型导致值超出范围。\r\n语句已终止
-
这似乎与原始问题无关。你解决了吗?
标签: c# entity-framework entity-framework-6 automapper