简体   繁体   English

EF Core 已跟踪具有相同 ID 的实体

[英]EF Core already tracked entity with same id

I can't figure out how to get rid of the error:我不知道如何摆脱错误:

The instance of entity type 'Relation' cannot be tracked because another instance with the key value '{Id: 26}' is already being tracked.无法跟踪实体类型“关系”的实例,因为已在跟踪另一个具有键值“{Id: 26}”的实例。 When attaching existing entities, ensure that only one entity instance with a given key value is attached.附加现有实体时,请确保仅附加一个具有给定键值的实体实例。

I tried detaching the entities from the context, but even that did not prevent this error from happening.我尝试将实体从上下文中分离出来,但即使这样也不能阻止这个错误的发生。 Can someone point out to me what I am doing wrong?有人可以向我指出我做错了什么吗?

Service:服务:

    private async Task<int> EditAsync(RelationModel model)
    {
        RelationEntity entity = _mapper.ToEntity(model);
        RelationEntity oldRelation = await _relationRepository.GetRelationAsync(entity.Id);
        _relationRepository.UpdateRelation(oldRelation, entity);
        await _relationRepository.SaveAsync();
        return entity.Id;
    }

Repository:存储库:

    public void UpdateRelation(Relation oldRelation, Relation relation)
    {
        if (oldRelation != null)
        {
            // Detach
            Context.Entry(oldRelation).State = EntityState.Detached;

            // Delete childs
            if (oldRelation.Person != null && relation.Person == null)
            {
                Context.Persons.Remove(oldRelation.Person);
            }

            if (oldRelation.Customer != null && relation.Customer == null)
            {
                Context.Customers.Remove(oldRelation.Customer);
            }

            if (oldRelation.Supplier != null && relation.Supplier == null)
            {
                Context.Suppliers.Remove(oldRelation.Supplier);
            }

            if (oldRelation.Employee != null && relation.Employee == null)
            {
                Context.Employees.Remove(oldRelation.Employee);
            }

            // Update parent
            Context.Relations.Update(relation); // <-- error occurs
        }
    }

Relation entity: Relation实体:

public class Relation : BaseEntity<int>
{
    public string Code { get; set; }

    public int? PersonId { get; set; }
    public Person Person { get; set; }

    public int? CompanyId { get; set; }
    public Company Company { get; set; }

    public int? CustomerId { get; set; }
    public Customer Customer { get; set; }

    public int? SupplierId { get; set; }
    public Supplier Supplier { get; set; }

    public int? EmployeeId { get; set; }
    public Employee Employee { get; set; }

    public ICollection<RelationRelations> ParentRelations { get; set; }
    public ICollection<RelationRelations> ContactPersons { get; set; }
    public ICollection<RelationContactMethod> ContactMethods { get; set; }
    public ICollection<RelationAddress> Addresses { get; set; }
}

UPDATE更新

I tried replacing我尝试更换

Context.Relations.Update(relation); 

with

Context.Entry(oldRelation).CurrentValues.SetValues(relation);

but then none of my properties on the Relation entity get updated.但是我在Relation实体上的所有属性都没有更新。

UPDATE 2更新 2

When running the following code运行以下代码时

Context.Entry(oldRelation).CurrentValues.SetValues(relation);
Context.SaveChanges();

I can see that old relation is updated with the new values, but they aren't being applied to the DB... Why is that?我可以看到旧关系已使用新值进行了更新,但它们并未应用于数据库……这是为什么呢?

I am using this code instead of "Update" for many many years, everything always was updated properly and I never had any errors.多年来,我一直在使用此代码而不是“更新”,一切都得到了正确的更新,而且我从来没有出现任何错误。

It is never good idea trying to use generic code for EF.尝试对 EF 使用通用代码绝不是一个好主意。 Try to select this way if you want to update several classes:如果要更新几个类,请尝试以这种方式 select:

 var oldrelation = Context.Relations
.Include(i=> i.Person)
.Include(i=> i.Company)
...
.FirstOrDefault(i=> i.Id=entity.Id);

Replace代替

 Context.Relations.Update(relation); 

with

Context.Entry(oldRelation).CurrentValues.SetValues(relation);

暂无
暂无

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

相关问题 EF:无法跟踪实体类型X的实例,因为已经跟踪了具有相同密钥的此类型的另一个实例 - EF: The instance of entity type X cannot be tracked because another instance of this type with the same key is already being tracked 无法跟踪实体类型“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 实体类型的实例 <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 无法跟踪实体类型 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
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM