The instance of entity type 'Table' 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.
This is the message I received when trying to write multiple records to database.
My codes work perfectly fine when there is only one record to be written to database.
So, this situation is quite weird.
Here is my code snippet
var newcompany = new Company();
newcompany.CompanyName = Name;
newcompany.Address = Address;
newcompany.Phone = Phone;
newcompany.City = City;
newcompany.Country = Country;
newcompany.Employees = 100;
newcompany.State = State;
_context.Entry(newcompany).State = EntityState.Modified;
_context.Dividend.Add(newcompany);
await _context.SaveChangesAsync();
Please notice that when there is one single record to be written into database, it works perfectly fine.
It only throws an exception when there are multiple records to be written into database.
Thank you for any suggestions.