zzzprojects / EntityFramework-Classic

Entity Framework Classic is a supported version of the latest EF6 codebase. It supports .NET Framework and .NET Core and overcomes some EF limitations by adding tons of must-haves built-in features.
https://entityframework-classic.net
Other
103 stars 27 forks source link

Current Values are not reset when using SoftDelete #64

Closed HenroOnline closed 2 years ago

HenroOnline commented 2 years ago

Hello,

I have an entity Folder with SubFolders. I am using a SoftDelete trigger, which works as expected. Except, the ParentFolderId is reset to NULL when a Child folder is deleted. This happens I think because I use DbSet.Remove(folder), and as a result it will remove the child folder from the parent entities Folder collection. The result of this is that the property ParentFolderId of the folder I want to delete is set to NULL. This change is also persisted to the database.

The workaround which I use now is to reset the CurrentValues. This does not feel like the correct way. Should this be something what EF Classic should be doing or does anyone have tips how te prevent this problem.

image

image

JonathanMagnan commented 2 years ago

Hello @HenroOnline ,

I'm not fully sure to understand as it looks to be expected behavior.

You are using the ChangeTracker, so if you remove a folder, this is expected that the relationship is also removed but perharp I'm missing something as well.

If you think that's an issue, can you provide us a runnable project with the minimum code to reproduce it. That will be easier for us to understand the issue with a code that we can run. It doesn’t need to be your project, just a new solution with the minimum code to reproduce the issue.

Your workaround looks indeed weird.

Best Regards,

Jon

HenroOnline commented 2 years ago

I expected that when I delete a folder, the other properties would not be changed. Here are some screenshots from the data. The field ParentFolderId is now NULL. Is there a way to detect these changes in relations?

Before delete image

After delete image

JonathanMagnan commented 2 years ago

Hello @HenroOnline ,

Thank you, I think I understand now.

I will try it on my side.

Best Regards,

Jon

JonathanMagnan commented 2 years ago

Hello @HenroOnline ,

We successfully reproduced the problem and made a fix.

We are currently testing our fix to determine if that's the best way to handle this scenario or not.

I will let you know more soon.

HenroOnline commented 2 years ago

@JonathanMagnan Thanks for looking into it!

JonathanMagnan commented 2 years ago

Hello @HenroOnline ,

Took us a lot of time but we finally succeed to release a new option for fixing this scenario.

When enabled the KeepRelationships options, the relation should not be removed anymore

context.Configuration.SoftDelete.KeepRelationships = true;

Let me know if everything is working as expected.

Best Regards,

Jon