Just following on from my web api/swagger/data .net core 2.2 to 3.1 upgrade and here. I hit another blocker just as everything was going so smoothly!. Basic functional testing on the API was going well until I noticed some POST operations failing with “ERROR The INSERT statement conflicted with the FOREIGN KEY constraint <FK>. The conflict occurred in database “DATABASE”, table “dbo.<tablename>”, column ‘<column name>’. “
This was all working nicely prior to the upgrade so what gives? After a bit of digging I was directed to the EF Core 3.0 Breaking Changes. Turns out temporary identities (that I’d relied on so much when creating entities with child entities) are no longer supported. I guess this would stop any potential negative values being persisted to data stores but didn’t really help me right now.
So what’s the solution? Hitting the DB several times to get parent Id then to decorate the child ids seems crazy. The breaking changes offers the following:
context.Entry(blog).Property(e => e.Id).CurrentValuewill return the temporary value even though
blog.Iditself hasn’t been set.
The first bullet point doesn’t really help matters and the last bullet point feels a bit messy. So I’m left with using navigational properties. This wasn’t too much of a ballache as the navigational properties should be there already so it’s just a case of replacing
someChildEntity.ParentId = someParentEntity.Id with someChildEntity.Parent = someParentEntity
This appears to resolve the issue but I guess always check the breaking changes before going for the upgrade to ensure any point estimates account for the additional work!