Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
sajadghobadi opened this issue May 19, 2022 · 1 comment

Comments

@sajadghobadi
Copy link

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

@mmmmosavi68
Copy link

Check if you have created this ID variable in the model that created it or not? Of course, if you inherit from BaseEntity or IEntity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants