Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PropertyValues.SetValues(obj) causes DELETE instead of UPDATE #16546

Closed
aureole82 opened this issue Jul 10, 2019 · 5 comments
Closed

PropertyValues.SetValues(obj) causes DELETE instead of UPDATE #16546

aureole82 opened this issue Jul 10, 2019 · 5 comments
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported Servicing-approved type-bug
Milestone

Comments

@aureole82
Copy link

Describe what is not working as expected.
DELETE FROM [ContainerRooms] WHERE [Id] = ...;
instead of
UPDATE [ContainerRooms] SET [ProductId] = NULL WHERE [Id] = ...;

Steps to reproduce

Eval.EFCore.DeleteInsteadUpdate.zip

Schema:
Container 1-n ContainerRoom n-1 Product

modelBuilder.Entity<ContainerRoom>()
    .HasOne(room => room.Product)
    .WithMany(product => product.Rooms)
    .HasForeignKey(room => room.ProductId)
    .IsRequired(false)
    .OnDelete(DeleteBehavior.Cascade)
    ;

Assume the following use case (where the room will be emptied):

var detachedRoom = detachedContainer.Rooms.First();
// Prepare update (remove product from room).
detachedRoom.Product = null;
detachedRoom.ProductId = null;

var attachedRoom = attachedContainer.Rooms.Single();
Console.WriteLine($"Before update: {db.Entry(attachedRoom).State}"); // Unchanged
// Update.
//attachedRoom.ProductId = null;
//attachedRoom.Product = null;
db.Entry(attachedRoom).CurrentValues.SetValues(detachedRoom);

Console.WriteLine($"After update: {db.Entry(attachedRoom).State}"); // Modified
await db.SaveChangesAsync(); // DELETE FROM [ContainerRooms] WHERE [Id] = @p0; ???
Console.WriteLine($"After persist: {db.Entry(attachedRoom).State}"); // Detached :-(

Instead of updating the ContainerRoom it will be deleted!

Workaround

attachedRoom.ProductId = null;
attachedRoom.Product = null;
await db.SaveChangesAsync(); // UPDATE [ContainerRooms] SET [ProductId] = @p0 WHERE [Id] = @p1;

Further technical details

EF Core version: 2.2.6
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Windows 10
IDE: Visual Studio 2019, Version 16.1.6

@ajcvickers
Copy link
Member

Note for triage: I was able to reproduce this. Will require some more investigation to figure out what is going wrong.

@ajcvickers
Copy link
Member

Note for triage: confirmed that this still repros in 3.0.

ajcvickers added a commit that referenced this issue Jul 15, 2019
Issue #16483 which is already fixed in 3.0
Issue #16546 which I thought might be fixed, but isn't
ajcvickers added a commit that referenced this issue Jul 15, 2019
Issue #16483 which is already fixed in 3.0
Issue #16546 which I thought might be fixed, but isn't
@ajcvickers ajcvickers self-assigned this Jul 19, 2019
@ajcvickers ajcvickers added this to the Backlog milestone Jul 19, 2019
@ajcvickers ajcvickers modified the milestones: Backlog, 5.0.0 Nov 13, 2019
ajcvickers added a commit that referenced this issue Dec 21, 2019
@ajcvickers ajcvickers added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Dec 21, 2019
ajcvickers added a commit that referenced this issue Dec 22, 2019
ajcvickers added a commit that referenced this issue Dec 22, 2019
@aureole82
Copy link
Author

aureole82 commented Dec 23, 2019

In which version is this fixed? Will the fix be published in Version 3.1?

@ajcvickers
Copy link
Member

@aureole82 The milestone on the issue indicates which release this is targeted for--currently 5.0. We may choose to patch 3.1 with this, but that's not currently the plan.

@aureole82
Copy link
Author

Consider to patch 3.1, please!
Otherwise the complete Long Term Support promise makes no sense to me.

@ajcvickers ajcvickers reopened this Feb 8, 2020
@ajcvickers ajcvickers modified the milestones: 5.0.0, 3.1.x Feb 8, 2020
ajcvickers added a commit that referenced this issue Feb 13, 2020
ajcvickers added a commit that referenced this issue Feb 13, 2020
Issue #16483 which is already fixed in 3.0
Issue #16546 which I thought might be fixed, but isn't
ajcvickers added a commit that referenced this issue Feb 13, 2020
…elete issues

We made tweeks to the cascade delete behavior in 3.0 and also changed the default timing for when cascades happen.
This change introduced several bugs which all result in deletion or severing of relationships instead of detaching them.
This was then hit by more people due to the timing change.

Issues: #19203 #19137 #18982 #16546
ajcvickers added a commit that referenced this issue Feb 13, 2020
…elete issues

We made tweeks to the cascade delete behavior in 3.0 and also changed the default timing for when cascades happen.
This change introduced several bugs which all result in deletion or severing of relationships instead of detaching them.
This was then hit by more people due to the timing change.

Issues: #19203 #19137 #18982 #16546
ajcvickers added a commit that referenced this issue Feb 15, 2020
ajcvickers added a commit that referenced this issue Feb 15, 2020
Issue #16483 which is already fixed in 3.0
Issue #16546 which I thought might be fixed, but isn't
ajcvickers added a commit that referenced this issue Feb 15, 2020
…elete issues

We made tweeks to the cascade delete behavior in 3.0 and also changed the default timing for when cascades happen.
This change introduced several bugs which all result in deletion or severing of relationships instead of detaching them.
This was then hit by more people due to the timing change.

Issues: #19203 #19137 #18982 #16546
@ajcvickers ajcvickers modified the milestones: 3.1.x, 3.1.3 Feb 15, 2020
@ajcvickers ajcvickers removed their assignment Sep 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported Servicing-approved type-bug
Projects
None yet
Development

No branches or pull requests

3 participants