efcore: Batched query breaks after upgrade to 7.0 - The UPDATE statement conflicted with the FOREIGN KEY constraint
After upgrading to version 7.0 of EntityFramework, we are encountering an error. The error thrown is The UPDATE statement conflicted with the FOREIGN KEY constraint.
Minimal extract from actual code:
foreach (var salesOrder in salesOrders)
{
var delivery = new Delivery
{
AddressId = salesOrder.ShippingAddressId,
CustomerName = salesOrder.Name,
DeliveryDate = date,
Position = -1,
Route = route,
TimeslotEnd = deliveryDay?.DeliveryTo,
TimeslotStart = deliveryDay?.DeliveryFrom
};
Context.Deliveries.Add(delivery);
// add reference from PackingSlip to Delivery
if (source == ConfigureDeliverySource.PackingSlips)
{
var packingSlip = packingSlips.First(p => salesOrder.PackingSlipNumber == p.Number);
delivery.PackingSlips.Add(packingSlip);
}
}
await Context.SaveChangesAsync().ConfigureAwait(false);
This works OK as long as there are only a few Delivery entities to insert, however, after 42 entities, EF creates multiple inserts, and that’s when it apparently goes wrong. When it subsequently tries to update the PackingSlip entities to set the DeliveryId reference, there is now an incorrect reference (see @p651):
[Parameters=[@p652='1054315', @p651='-2147482352' (Nullable = true), @p654='1054316', @p653='283504' (Nullable = true), @p656='1054317', @p655='283505' (Nullable = true), @p658='1054318', @p657='283505' (Nullable = true), @p660='1054319', @p659='283504' (Nullable = true), @p662='1054320', @p661='283504' (Nullable = true), @p664='1054321', @p663='283504' (Nullable = true), @p666='1054322', @p665='283504' (Nullable = true), @p668='1054323', @p667='283504' (Nullable = true), @p670='1054324', @p669='283504' (Nullable = true), @p672='1054325', @p671='283504' (Nullable = true), @p674='1054326', @p673='283504' (Nullable = true), @p676='1054327', @p675='283504' (Nullable = true), @p678='1054328', @p677='283504' (Nullable = true), @p680='1054329', @p679='283504' (Nullable = true), @p682='1054330', @p681='283504' (Nullable = true), @p684='1054331', @p683='283504' (Nullable = true), @p686='1054332', @p685='283504' (Nullable = true), @p688='1054333', @p687='283504' (Nullable = true), @p690='1054334', @p689='283504' (Nullable = true), @p692='1054335', @p691='283504' (Nullable = true), @p694='1054336', @p693='283504' (Nullable = true), @p696='1054337', @p695='283504' (Nullable = true), @p698='1054338', @p697='283504' (Nullable = true), @p700='1054339', @p699='283506' (Nullable = true), @p702='1054340', @p701='283506' (Nullable = true), @p704='1054341', @p703='283506' (Nullable = true), @p706='1054342', @p705='283507' (Nullable = true), @p708='1054343', @p707='283508' (Nullable = true), @p710='1054344', @p709='283509' (Nullable = true), @p712='1054345', @p711='283509' (Nullable = true), @p714='1054346', @p713='283509' (Nullable = true), @p716='1054347', @p715='283509' (Nullable = true), @p718='1054348', @p717='283509' (Nullable = true), @p720='1054349', @p719='283509' (Nullable = true), @p722='1054350', @p721='283509' (Nullable = true), @p724='1054351', @p723='283509' (Nullable = true), @p726='1054352', @p725='283510' (Nullable = true), @p728='1054353', @p727='283511' (Nullable = true), @p730='1054354', @p729='283512' (Nullable = true), @p732='1054355', @p731='283513' (Nullable = true), @p734='1054356', @p733='283514' (Nullable = true)], CommandType='Text', CommandTimeout='300']
SET NOCOUNT ON;
UPDATE [PackingSlips] SET [DeliveryId] = @p651
OUTPUT INSERTED.[Checksum]
WHERE [PackingSlipId] = @p652;
UPDATE [PackingSlips] SET [DeliveryId] = @p653
OUTPUT INSERTED.[Checksum]
WHERE [PackingSlipId] = @p654;
// shortened for brevity
Include provider and version information
EF Core version: 7.0.0 Database provider: Microsoft.EntityFrameworkCore.SqlServer Target framework: net7.0 Operating system: Windows 11 IDE: Visual Studio 2022 17.4
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 15 (9 by maintainers)
After updating the
NuGet.configfile to thedotnet8feed, I tested it against version8.0.0-alpha.1.22570.1which does indeed work. Thanks again for your efforts. For now we’ll use the workaround so we can use the stable7.0.0packages until this fix is included in a release.I’ve emailed you a sample that should fail under
7.0.0.