Skip to content
This repository has been archived by the owner on Jan 16, 2024. It is now read-only.

WillCascadeOnDelete not working with EF Core #22

Closed
lizardking opened this issue Sep 18, 2018 · 4 comments
Closed

WillCascadeOnDelete not working with EF Core #22

lizardking opened this issue Sep 18, 2018 · 4 comments
Labels
bug Confirmed bug

Comments

@lizardking
Copy link

Hi
When I define relations between entities and set End 1 on delete resp. End 2 on delete to Cascade or None the EFDesigner creates a .WillCascadeOnDelete(true/false). This did not compile on my system. AFIK WillCascadeOnDelete does not exist for EF core (it does in EF6) and OnDelete(DeleteBehavior) has to be used for EF Core.

Replacing the code to generate WillCascadeOnDelete for the for undirectionalassociations with

switch(association.SourceDeleteAction) {
  case DeleteAction.Default:
    //Nothing to do
    break;
  case DeleteAction.Cascade:
    segments.Add("OnDelete(DeleteBehavior.Cascade)");
    break;
  case DeleteAction.None:
    segments.Add("OnDelete(DeleteBehavior.Restrict)");
    break;
}

and for the bidirectionalassociations with

switch(association.SourceDeleteAction) {
  case DeleteAction.Default:
    //Nothing to do
    break;
  case DeleteAction.Cascade:
    segments.Add("OnDelete(DeleteBehavior.Cascade)");
    break;
  case DeleteAction.None:
    segments.Add("OnDelete(DeleteBehavior.Restrict)");
    break;
}

in the EFCoreDesigner.ttinclude seems to fix the problem. Not sure if this would be valid for all use cases.

Small side note:
I'm not sure if I got this right, but for Bidirectionalassociations the docu on End 1 on delete resp. End 2 on delete says How to handle objects on this end if the object on the other end is deleted. However the T4 files for EF core, seem to handle this more like How to handle objects on the other end if the object on this end is deleted.

@msawczyn msawczyn added bug Confirmed bug investigating Looking into this and removed bug Confirmed bug labels Sep 18, 2018
@msawczyn
Copy link
Owner

Thanks for the bug report. I'm looking into this; cascade delete support has always been a bit problematic and since you're bringing it up, it's a good time to review it again.

You've got a good start on the fix. There's a bit more to do, since really the flag deals with the principal and dependent ends of the relationship, and that's not exposed nicely in the designer. So I'll do a bit more with the design surface as well as the T4.

If all goes right, this fix should be out in v1.2.6.7

@lizardking
Copy link
Author

Thanks for taking care of this.

Just as a additional remark/question:
I dont understand why there are End1 On Delete and End2 On Delete for the assosiations in the designer? In the DB there is, IMHO for obvious reasons, only one delete action. If there is no specific reason for End1 On Delete and End2 On Delete it might be a good idea to just have one On Delete definition (like in the DB). I guess that would be easier to understand for the users.

@msawczyn msawczyn added bug Confirmed bug and removed investigating Looking into this labels Sep 19, 2018
@msawczyn
Copy link
Owner

The concept is to propagate deletion to the other end of the association depending on which end is deleted. So, if a <--> b, where a is End1 and b is End2 (which will be obvious from other data in the property editor), when cascade is requested on End1, deleting a also deletes b. If cascade is requested on End2, deleting b also deletes a.

While technically we're deleting associations, that's not the common way to consider object deletion. Rather, it's just that ... object deletion, which is why there cascade behaviors are defined on the classes that are at the ends of the associations.

That being said, EF's cascade delete behavior is currently only defined for removing dependent objects, not the other way around. That's a blemish in the designer, and I'm planning to refine what's displayed in the property editor to make that clear, with parallel changes in code generation.

Good article re: cascade delete in EF6 (with some discussion of EFCore) at https://weblogs.asp.net/ricardoperes/entity-framework-pitfalls-cascade-deletes

Hope that makes sense.

@msawczyn
Copy link
Owner

Fixed in v1.2.6.7. Took what you offered and added a few more enhancements to the property editor so that it was clearer what was going to happen in the generated code. And I'll be adding more documentation on this -- it's currently extremely wimpy in this area -- as soon as I have a moment.

Thanks!

msawczyn added a commit that referenced this issue Sep 24, 2018
   - An entity's concurrency token property is no longer a required parameter in its constructor (#24)
   - Simplified cascade delete settings in property editor for associations
   - Fixed bad code generation in EFCore for cascade delete overrides (#22)
   - Missing files when generating code for .NET Core projects fixed
   - Tightened up and swatted some bugs in INotifyPropertyChanged handling. Added documentation to doc site for this feature (following up on #23)
   - Ensured multiline editing was available in property window for those properties that made sense
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Confirmed bug
Projects
None yet
Development

No branches or pull requests

2 participants