Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
The instance of entity type SomeType cannot be tracked because another instance with the same key value for SomeField 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.
Microsoft.EntityFrameworkCore.ChangeTracking.Internal.IdentityMap<TKey>.ThrowIdentityConflict(InternalEntityEntry entry)


What I have tried:

I tried calling same entity twice:

public async Task<IActionResult> OnPostAsync(int? id)
{
var someClassVarFromTheRazorPage = SomeClass_IdentityFromTheRazorPage; 
//this comes as a Entity Model from the Razor Page and has some valuse
			
SomeClass someClass = new SomeClass(); 
//this is second instance initialized

someClass = await _context.SomeClass.FirstOrDefaultAsync(m => m.Id == id);
//this is second instance with values from DB

SomeClass_IdentityFromTheRazorPage.Time = someClass.Time; 
//assigning Time value from someClass with Db value to SomeClass_IdentityFromTheRazorPage with Page value
			
_context.Attach(SomeClass_IdentityFromTheRazorPage); // Error
await _context.SaveChangesAsync();

return RedirectToPage("/SomeClassIndex/SomeClassDetail", new { id = SomeClass_IdentityFromTheRazorPage.JobNo.ToString() });
}
Posted
Updated 16-Sep-22 6:46am
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900