Skip to content

cannot be tracked... error for update entity with collection navigation property  #421

@sajadghobadi

Description

@sajadghobadi

for example if your entity has property same as
public ICollection<DiseaseAttachment> Attachments { get; set; }
in update method in your controller you must load it by using LoadCollectionAsync
in my usecase :
var model = await Repository.GetByIdAsync(cancellationToken, dto.Id); await Repository.LoadCollectionAsync(model, x => x.Attachments, cancellationToken);
but problem is here that exception occurred
`
model = dto.ToEntity(Mapper, model);

        await Repository.UpdateAsync(model, cancellationToken);

`

exception is this :

System.InvalidOperationException : The instance of entity type 'DiseaseAttachment
' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked. When attac
hing existing entities, ensure that only one entity instance with a given key value is attached. Consider using 'DbC
ontextOptionsBuilder.EnableSensitiveDataLogging' to see the conflicting key values

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions