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)
-
0
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
-
0
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 :)
-
0
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 theContact
entity. -
0
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.
-
0
Sure, my github account: https://github.com/EngincanV
-
0
Hi,
I have sent you an invite :)
-
0
Hi @Neozzz, I've accepted your invitation and examine the project but I could not see the related entities (Employee, Contact and Address).
-
0
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 :)
-
0
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.
-
0
Hi,
I have sent you the mail. Please let me know if further info is needed.
Regards,
-
0
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.
-
0
Hi @EngincanV,
We're still getting the error. Do you mind checking remotely?
Thanks
-
0
Hi @Neozz, I've made one small change to the code and create a PR. Please examine it, it should be fixed now.
-
0
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 :)
-
0
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.