© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4y ago•
4 replies
pyrodistic

InvalidOperationException - EF Instance - Identity Resolution

I'm getting:
InvalidOperationException: The instance of entity type 'Vet' 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.
InvalidOperationException: The instance of entity type 'Vet' 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.

 public async Task<IActionResult> Create(RoomViewModel model)
        {
            if (ModelState.IsValid)
            {
                // Converts the model to a Room object.
                var room = _converterHelper.ToRoom(model, true);

                // Before creating the Room check if there's a room with the same VetId, nulls it if exists.
                var previousRoom = _roomRepository.GetByVet((int)model.VetId).AsNoTracking().FirstOrDefault();
                if (previousRoom != null)
                {
                    previousRoom.VetId = null;
                    await _roomRepository.UpdateAsync(previousRoom);
                }
                
                // Creates Room
                await _roomRepository.CreateAsync(room);

                // Changes the room on the related Vet object if provided.
                if(room.VetId != null)
                {
                    Vet vet = await _vetRepository.GetByIdAsync((int)room.VetId);

                    await _vetRepository.UpdateAsync(vet); //Error occurs at this point.
                }

                return RedirectToAction(nameof(Index));
            }
            return View(model);
        }
 public async Task<IActionResult> Create(RoomViewModel model)
        {
            if (ModelState.IsValid)
            {
                // Converts the model to a Room object.
                var room = _converterHelper.ToRoom(model, true);

                // Before creating the Room check if there's a room with the same VetId, nulls it if exists.
                var previousRoom = _roomRepository.GetByVet((int)model.VetId).AsNoTracking().FirstOrDefault();
                if (previousRoom != null)
                {
                    previousRoom.VetId = null;
                    await _roomRepository.UpdateAsync(previousRoom);
                }
                
                // Creates Room
                await _roomRepository.CreateAsync(room);

                // Changes the room on the related Vet object if provided.
                if(room.VetId != null)
                {
                    Vet vet = await _vetRepository.GetByIdAsync((int)room.VetId);

                    await _vetRepository.UpdateAsync(vet); //Error occurs at this point.
                }

                return RedirectToAction(nameof(Index));
            }
            return View(model);
        }


After restarting the application everything seems to be correctly updated despite the error. I've read a couple of stackoverflow posts, but I couldn't figure out what to do in order to fix this.

Would appreciate any help.
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

✅ EF Core 8: System.InvalidOperationException: 'The instance of entity type cannot be tracked..
C#CC# / help
10mo ago
Identity/EF collection returns null
C#CC# / help
2y ago
Reset identity colums EF Core error.
C#CC# / help
16mo ago
✅ Mapping an Identity user to an instance of an EF core model ID
C#CC# / help
4y ago