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
Markdown supported.Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post) -
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 :)
Markdown supported.Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post) -
0
If you add your contacts through the
Employeeentity 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
Employeeentity like you do for theContactentity.Markdown supported.Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post) -
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.
Markdown supported.Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post) -
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 :)
Markdown supported.Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post) -
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.
Markdown supported.Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post) -
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.
Markdown supported.Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post) -
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 :)
Markdown supported.Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post) -
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.
Markdown supported.Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)