Description
I have created a reproduction at https://github.com/Tragetaschen/ef-many-to-many-update
I have a many-to-many relation between One
and Two
where the explicit join table contains an Order
property.
In the entry point, I setup a database with one One
referencing three Two
s and order the references 0
, 1
, and 2
respectively.
I then want to update the referenced Two
s: The first two swap their Order
and the one with order 2
is now a fourth Two
instead.
After running the program, I expect the following output
Order: 0; Two: 2
Order: 1; Two: 1
Order: 2; Two: 4
However, the actual output is
Order: 1; Two: 1
Order: 1; Two: 2
Order: 2; Two: 4
where the Order
property has duplicated values.
I have been able to trace this to the HasDefaultValue(0)
call in the OnModelCreating
method. Without it, the output matches the expectation. That configuration had been added when the field was introduced because of existing data in the join table.
Additionally, if I start the new ordering at 1
as opposed to 0
everything also works as expected:
Order: 1; Two: 2
Order: 2; Two: 1
Order: 3; Two: 4
Include provider and version information
EF Core version: 7.0.4
Database provider: SqlServer (actual application), Sqlite (reproduction)
Target framework: .NET 7 (actual application), .NET 8.0 (reproduction)
Operating system: all
IDE: Visual Studio 2022 17.6.0 Preview 2.0