Starts in:
2 DAYS
0 HR
28 MIN
49 SEC
Starts in:
2 D
0 H
28 M
49 S
Open Closed

"The instance of entity type 'EntityName' cannot be tracked" on POST #1920


User avatar
0
Neozzz created

We have the following relationships:

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

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

An employee can have multiple contacts and each contact can have multiple addresses. The following is a snippet of the json request for POST

{
"FirstName":"Neo",
"LastName":"Xav",
"Age":"20",
"Contact":[
{...},
{...},
{"Address":[{...},{...}]}
]
}

However I am getting the below error from logs when I use the CreateAsync method: The instance of entity type 'Contact' 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.

Please let me know what might be causing the issue.

Thank you

  • ABP Framework version: v4.4.2
  • UI type: swagger
  • DB provider: EF Core

15 Answer(s)
  • User Avatar
    0
    EngincanV created
    Support Team .NET Developer

    Hi @Neozzz, does your problem be related with https://github.com/abpframework/abp/issues/2660?

    • Please check your entities have a constructor with an id parameter passed by the base class. https://docs.abp.io/en/abp/latest/Entities#entities-with-guid-keys https://docs.abp.io/en/abp/latest/Best-Practices/Entities#primary-constructor
  • User Avatar
    0
    Neozzz created

    Hi @EngincanV,

    I added the following lines to the Contact domain class:

    protected Contact() { } internal Contact (Guid id) : base(id) { }

    The error still shows. Is there something I am missing?

    Thank you :)

  • User Avatar
    0
    EngincanV created
    Support Team .NET Developer

    If you add your contacts through the Employee entity as below (by using Contact collection)

    var employee = new Employee();
    employee.Contacts.Add(new Contact()); 
    await _employeeRepository.CreateAsync(employee);
    

    you also need to create constructor with id parameter for Employee entity like you do for the Contact entity.

  • User Avatar
    0
    Neozzz created

    the id param is there for employee entity. i am using the manager class in between and i have put all the properties inside the constructor for employee entity. if you could share your github id, i can share the repo with you.

  • User Avatar
    0
    EngincanV created
    Support Team .NET Developer

    Sure, my github account: https://github.com/EngincanV

  • User Avatar
    0
    Neozzz created

    Hi,

    I have sent you an invite :)

  • User Avatar
    0
    EngincanV created
    Support Team .NET Developer

    Hi @Neozzz, I've accepted your invitation and examine the project but I could not see the related entities (Employee, Contact and Address).

  • User Avatar
    0
    Neozzz created

    Hi engincan, The names are different. I did not want to put the exact name out in the public domain. Are you free for a remote session? or please share your mail id. Thanks :)

  • User Avatar
    0
    EngincanV created
    Support Team .NET Developer

    Hi engincan, The names are different. I did not want to put the exact name out in the public domain. Are you free for a remote session? or please share your mail id. Thanks :)

    You can send an email to engin.veske@volosoft.com. Thanks.

  • User Avatar
    0
    Neozzz created

    Hi,

    I have sent you the mail. Please let me know if further info is needed.

    Regards,

  • User Avatar
    0
    EngincanV created
    Support Team .NET Developer

    Hi @Neozzz, I've made the necessary changes on your repo and send a pull-request. You can examine the PR and if your problem is resolved you can close the question.

  • User Avatar
    0
    Neozzz created

    Hi @EngincanV,

    We're still getting the error. Do you mind checking remotely?

    Thanks

  • User Avatar
    0
    EngincanV created
    Support Team .NET Developer

    Hi @Neozz, I've made one small change to the code and create a PR. Please examine it, it should be fixed now.

  • User Avatar
    0
    Neozzz created

    Hi Engincan, I noticed it's suggested by you that to not use Guid.NewGuid() at the dto. What do you propose is a better alternative. If you could give us brief of what has to be done, it'd help us making the architectural change.

    Thank you :)

  • User Avatar
    0
    EngincanV created
    Support Team .NET Developer

    Hi @Neozzz, I will send you the steps to be done via email as soon as possible. If the last changes resolved your problem you can close this question. I will inform you via email.

Made with ❤️ on ABP v9.1.0-preview. Updated on November 20, 2024, 13:06