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

Can't use ON DELETE CASCADE #76

Closed
Azramis opened this issue May 20, 2019 · 5 comments
Closed

Can't use ON DELETE CASCADE #76

Azramis opened this issue May 20, 2019 · 5 comments
Labels
released Issue is resolved in a current release

Comments

@Azramis
Copy link

Azramis commented May 20, 2019

Hi,

I've tried to generate bi-directionnal table relation on one-to-many, with the "ON DELETE CASCADE", but it seems that it generated nothing to do that.
I had to put the code by myself.

.OnDelete(DeleteBehavior.Cascade);

Maybe I'm doing something wrong ? Eventually an bug ?

@msawczyn msawczyn added the investigating Looking into this label May 26, 2019
@dcastenholz
Copy link
Contributor

I think that if you change the generation code for UnidirectionalAssociation to:

if (association.SourceDeleteAction != DeleteAction.Default) { string willCascadeOnDelete = (association.SourceDeleteAction == DeleteAction.Cascade).ToString().ToLowerInvariant(); segments.Add($"WillCascadeOnDelete({willCascadeOnDelete})"); }

it might fix it.

@msawczyn
Copy link
Owner

msawczyn commented Jun 4, 2019

Nice. I'll put that on the list of things to hit for the next release. I'm working the IdentityDbContext right now and am pretty focused on that, but from the investigation you've done it appears to be a quick fix.

@Azramis
Copy link
Author

Azramis commented Jun 5, 2019

Thanks guys for response.
@dcastenholz My problem is on BidirectionnalAssociation, not sure that your fix apply to me.

In my case I change the generation code for Bidirectionnal into this:

if ((/*association.SourceDeleteAction != DeleteAction.Default &&*/ association.SourceRole == EndpointRole.Principal) ||
             (/*association.TargetDeleteAction != DeleteAction.Default &&*/ association.TargetRole == EndpointRole.Principal))
{
		 
    DeleteAction deleteAction = (association.SourceRole == EndpointRole.Principal)
                                          ? association.SourceDeleteAction
                                          : association.TargetDeleteAction;
    switch (deleteAction)
    {
         case DeleteAction.None:
            segments.Add("OnDelete(DeleteBehavior.Restrict)");
            break;
         case DeleteAction.Cascade:
            segments.Add("OnDelete(DeleteBehavior.Cascade)");
            break;
	 default:
            segments.Add("OnDelete(DeleteBehavior.SetNull)");
            break;
   }
}

It generate the .OnDelete(DeleteBehavior.Cascade);.

You can see that I manage default case manually, in order to fix a bug when using SQLite with EFCore. Default value for OnDelete in EFCore is ClientSetNull, but this value seems not be applicated on SQLite (RESTRICT is applicated...), so I set the "different but same" SetNull.

Maybe my fix is not good, but it works for me.

@msawczyn
Copy link
Owner

msawczyn commented Jul 9, 2019

Fixed in v1.3.0.4 - thanks!

@msawczyn msawczyn added pending release Issue is resolved in the current codebase, will be published with the next release released Issue is resolved in a current release and removed investigating Looking into this pending release Issue is resolved in the current codebase, will be published with the next release labels Jul 9, 2019
@msawczyn
Copy link
Owner

Closing since fix was released.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
released Issue is resolved in a current release
Projects
None yet
Development

No branches or pull requests

3 participants