Open Closed

cannot be tracked because another instance with the same key value for {'Id'} is already being tracked. When attaching existing entities, ensure that only one entity instance with a given key value is attached. #1971


User avatar
0
Neozzz created

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

Consider the following domain models:

public class Employee : FullAuditedEntity<Guid>
{
    <<<<properties....>>>
    public ICollection<Contact> Contacts {get;set;}   
}

public class Contact : FullAuditedEntity<Guid>
{
    <<<properties...>>>
    public ICollection<Address> Addresses {get;set;}
}

We're trying to update employee info and the addresses associated with the employee with the put api. However we're getting this error when trying to update Addresses.

we're using individual repos for updating using updateasync and updatemanyasync.

Please let me know what might be the issue.

Thank you


12 Answer(s)
  • User Avatar
    0
    Neozzz created

    hi. any updates?

  • User Avatar
    0
    gterdem created
    Senior .NET Developer

    This is not related with Abp. It is about about EfCore tracking db context trying to update related entities over parent entity.

    Please check StackOverflow for similar existing questions.

  • User Avatar
    0
    Neozzz created

    as usual useless answer from gterdem. please assign the ticket to someone else.

    we're using updateasync from volo.abp namespace. guess thats enough to make it fall under abp.

  • User Avatar
    0
    alper created
    Support Team Director

    This is not related to the ABP Framework. ABP Framework uses other 3rd party libraries like EF Core. And there's no support for the 3rd party questions. you can ask it on the EF Core repository https://github.com/dotnet/efcore thanks for your understanding

  • User Avatar
    0
    Neozzz created

    I am using updateasync from Volo.Abp.Domain.Repositories for updating the record. And when am using something from your own namespace from where else will I get support?

    Why should EF Core repo respond to me when I am using abp class when I could be using EF Core directly and not use any abp product at all?

    I have faced similar issue with POST but your team mates helped resolve it. And the answer had to do with using repository classes from abp.

  • User Avatar
    0
    gterdem created
    Senior .NET Developer

    as usual useless answer from gterdem.

    I am sorry if you are disappointed with my answer. Also i was not aware that I usually give useless answers. I was trying to point out the efficient ways to solve these kind of generic questions for you and for other users that may have the same issue with you in the near future.

    However, being toxic is never good and never result with expected good result in any branch of any job. Toxic behaviour magnets other toxic people that can de-motivate you more then you think. Think about colleague stressing you with questions like
    "How do you get paid if you need to ask this kind of question?", "At least don't write it down so that people shouldn't know the degree of your ignorance" or other non-constructive, negative and useless comments. That wouldn't be a healthy workplace nor a nice time to spend on. This may not make any sense to you right now but probably it will, after 5-10 years.

    That being said, we answered tens maybe hundreds of unrelated questions in support when asked properly as something like "Ok, I know this seems related with EfCore since i checked google but can you help me about ..."

    please assign the ticket to someone else.

    I am not sure how insulting will motivate a co-worker to help you about an unrelated 3rd party question.

  • User Avatar
    0
    Neozzz created

    @gterdem

    idk how replying with with statements like "go to stackoverflow" will warrant any respect to you either. I have had good experiences with your colleagues. Have raised nearly 20 questions here. All have been supportive and very helpful. Unfortunately except you. May be please be self reflective.

    I am sorry your colleagues might've asked those questions to you. And your passive aggressive statements doesn't look good on you :)

    That being said, we answered tens maybe hundreds of unrelated questions in support when asked properly as something like "Ok, I know this seems related with EfCore since i checked google but can you help me about ..."

    Rather than lecturing me on how to beg to you to get asnwers, may be you should emulate the work culture and tenacity shown by your colleagues to the work and learn from them. You could've asked for more info rather than just blurting out "stackoverflow" and closing the ticket just like that.

  • User Avatar
    1
    alper created
    Support Team Director

    you can use .AsNoTracking() in your Queryable object.

  • User Avatar
    0
    Neozzz created

    HI albert,

    Thank you for the reply. Could you please take a look at this link: https://docs.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.dbcontextoptionsbuilder.usequerytrackingbehavior?view=efcore-3.1

    An excerpt from the link:

    "Disabling change tracking is useful for read-only scenarios because it avoids the overhead of setting up change tracking for each entity instance. You should not disable change tracking if you want to manipulate entity instances and persist those changes to the database using SaveChanges()."

    Without disabling tracking is there a better way to solve this issue? For example, any sample in abp that allows us to change the entity state and then update?

    Thank you

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    I need to reproduce the problem then I can help you solve it. could you share full steps? it will be helpful.

  • User Avatar
    0
    Neozzz created

    hi @liangshiwei,

    I'll share the repo with you. I'll explain the issue via mail or remote; which ever is comfortable for you. Either ways, pls lemme know.

    Ty :)

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    You can share the repo to me: shiwei.liang@volosoft.com, thanks.

Made with ❤️ on ABP v9.1.0-preview. Updated on November 18, 2024, 05:54