简体   繁体   English

实体类型的实例 <T> 无法跟踪,因为已经跟踪了另一个具有相同的{&#39;Id&#39;}键值的实例

[英]The instance of entity type <T> cannot be tracked because another instance with the same key value for {'Id'} is already being tracked

I'm working on a function that is supposed to move records from three tables to their Archive* versions. 我正在使用一个功能,该功能应该将记录从三个表移至其Archive *版本。 The tables are Issues, Actions, Attachments. 这些表是问题,操作,附件。 First I get all records from the three tables, then make their Archive versions and attach these to the context: 首先,我从三个表中获取所有记录,然后创建其存档版本并将其附加到上下文:

var issue = _ctx.Issues.FirstOrDefault(x=>x.Id == issueId);
var actions = _ctx.Actions.Where(x=>x.IssueId == issueId).ToList();
var attachments = _ctx.Attachments.Where(x=>x.IssueId == issueId).ToList();

_ctx.Attachments.RemoveRange(attachments);
_ctx.Actions.RemoveRange(actions);
_ctx.Issues.Remove(issue);
await _ctx.SaveChangesAsync();

var archiveIssue = new ArchiveIssue(issue);
_ctx.ArchiveIssues.Add(archiveIssue); // this line throws the exception
_ctx.ArchiveActions.AddRange(actions.Select(x=>new ArchiveAction(x)));
_ctx.ArchiveAttachments.AddRange(attachments.Select(x=>new ArchiveAttachment(x)));
await _ctx.SaveChangesAsync();

trx.Commit();

I've checked the archiveIssue entity before the Add() method and its Id field has the proper value. 在Add()方法及其ID字段具有正确的值之前,我已经检查了archiveIssue实体。 There are no other records in the table with that value. 表中没有其他具有该值的记录。 I've already tried several other version, including AsNoTracking(), cloning the issue entity, or setting its state to Deleted. 我已经尝试过其他几个版本,包括AsNoTracking(),克隆问题实体或将其状态设置为“已删除”。

Archive* tables does have a key and they are set to ValueGeneratedNever() like: Archive *表确实具有键,并且将它们设置为ValueGeneratedNever(),例如:

modelBuilder.Entity<ArchiveIssue>()
        .HasKey(c => c.Id);

modelBuilder.Entity<ArchiveIssue>()
        .Property(c => c.Id)
        .ValueGeneratedNever();

ArchiveIssue constructor just copies all values from the source entity to the Archive one: ArchiveIssue构造函数只是将所有值从源实体复制到Archive一个:

public ArchiveIssue(Issue issue) {
        this.Id = issue.Id;
        this.Code = issue.Code;     
        this.WeekNo = issue.WeekNo;
        this.WeekCount = issue.WeekCount;
        this.CreateDate = issue.CreateDate;
        ...
}

The error message was just the top of many errors and the main causes were: 该错误消息只是许多错误的开头,主要原因是:

  • I had a foreign key in ArchiveAttachment to the original Issue (not the archive one). 我在原始发行版的ArchiveAttachment中有一个外键(而不是档案库中的一个)。 This caused that the entity of ArchiveAttachment created then deleted immediately because of cascade delete set in the FK property. 这导致创建的ArchiveAttachment实体随后由于FK属性中设置的级联删除而立即删除。
  • Had a similar error in ArchiveActions: I had a this.Issue = action.Issue line in ArchiveAction's ctor and that action.Issue was just the original Issue not the Archive one which is deleted in the next step. 在ArchiveActions中也有类似的错误:我有一个this.Issue = action.ArchiveAction的ctor中的issue行和那个action.Issue只是原始的Issue,而不是下一步要删除的Archive。
  • The op error was thrown by the cross foreign key I think but I don't understand exactly why. 我认为交叉异形键引发了操作错误,但我不知道为什么。 Anyway after fixing the FK it's gone. 无论如何,修复FK之后它就消失了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法跟踪实体类型“Entity”的实例,因为已跟踪另一个具有与 {'Id'} 相同键值的实例 - The instance of entity type 'Entity' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked 无法跟踪实体类型 Model 的实例,因为已跟踪另一个具有相同 {&#39;Id&#39;} 键值的实例 - The instance of entity type Model cannot be tracked because another instance with the same key value for {'Id'} is already being tracked 无法跟踪实体类型“Bus”的实例,因为已经在跟踪具有相同键值 {'Id'} 的另一个实例 - The instance of entity type ‘Bus’ cannot be tracked because another instance with the same key value for {‘Id’} is already being tracked 无法跟踪实体类型“AppUser”的实例,因为已跟踪另一个具有与 {'Id'} 相同键值的实例 - The instance of entity type 'AppUser' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked 无法跟踪实体类型“IdentityUser”的实例,因为已跟踪另一个具有与 {'Id'} 相同键值的实例 - The instance of entity type 'IdentityUser' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked 无法跟踪实体类型“书签”的实例,因为已经在跟踪具有相同键值 {'ID'} 的另一个实例 - The instance of entity type 'Bookmark' cannot be tracked because another instance with the same key value for {'ID'} is already being tracked 无法跟踪实体类型“Item”的实例,因为已跟踪另一个具有与 {'Id'} 相同键值的实例 - The instance of entity type 'Item' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked 无法跟踪实体类型的实例,因为已跟踪另一个具有与 {'Id'} 相同键值对的实例 - The instance of the entity type cannot be tracked because another instance with the same key value pair for{'Id'} is already being tracked 无法跟踪实体类型“ Article”的实例,因为已经跟踪了另一个具有相同键值的{&#39;Id&#39;}实例 - The instance of entity type 'Article' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked 无法跟踪实体类型“用户”的实例,因为已经在跟踪另一个具有相同键值的“Id”实例 - The instance of entity type 'User' cannot be tracked because another instance with the same key value for 'Id' is already being tracked
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM