Reputation: 447
I am simply building a model and then attempting to insert it to the database via the .SaveChanges()
function, but when I run SaveChanges()
I get a database error that a primary key duplicate has occurred...
so I dug into the SQL logging and found that this model insert is attempting to re-insert already existing data... why would that happen?
My code
using (var dbContext = new dbContext(_dbContextOptions))
{
Request request = new Request()
{
RequesterId = 1,
HelpRequestType = helpRequestTypeObject, //new model is being inserted for this on .SaveChanges()
Status = statusObject, //new model is being inserted for this on .SaveChanges()
AssignmentKeyId = 12,
TimeCreated = DateTime.Now,
CustomContactIds = null
};
//add request to transaction
dbContext.Request.Add(request);
dbContext.SaveChanges();
}
EDIT: So I changed everything to create the model only using ids for foreign key references (no directly linked entities) and this seems to have solved my issue. Once the entity is created and .SaveChanges() has been applied, the model has links to all other entities as expected. It seems I was causing myself problems by using entities that had been previously queried under a different DbContext than the active one I was using for this new entity creation.
Upvotes: 0
Views: 1271
Reputation: 5254
But it appears the issue was that I was directly linking existing entity models to the new model instead of linking them by ID...
I was querying for existing entities from the database and linking them to this new Request object directly rather than just by ID... This resulted in Entity Framework attempting to remake all the existing entities...
When you are using entities from more then one DbContext
, you need to be careful. If you query a HelpRequestType
object from contextA
, and connect it to an object belonging to contextB
, then contextB
has not been tracking the HelpRequestType
object and has no way of knowing, that the object is already part of the underlying database. It therefore considers the object as new. This is even true, if both contexts are of the same derived type.
If you need to use two different DbContext
objects for some reason, you need to Attach() the HelpRequestType
object of contextA
to contextB
first, before calling SaveChanges()
on contextB
.
Generally, it is way easier to just work with a single DbContext
object for all operations (unit of work pattern). You should have a good reason (performance, a model that is partitioned over multiple data stores, etc.) for using more then one DbContext
.
See also Working with Disconnected Entity Graph in Entity Framework Core for further details.
Upvotes: 1
Reputation: 447
So I'm not exactly sure why it was the issue, as I thought I had seen this done elsewhere... But it appears the issue was that I was directly linking existing entity models to the new model instead of linking them by ID...
I was querying for existing entities from the database and linking them to this new Request object directly rather than just by ID... This resulted in Entity Framework attempting to remake all the existing entities...
I changed the places I was using models instead of IDs and it all appears to be working now.
Upvotes: 0