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

Bahavior for Cascading deletes? #37

Open
JorgenLindell opened this issue Jan 24, 2019 · 3 comments
Open

Bahavior for Cascading deletes? #37

JorgenLindell opened this issue Jan 24, 2019 · 3 comments
Assignees

Comments

@JorgenLindell
Copy link

Are triggers capable of handling the cascading delete that occurs if you remove records from a navigation collection where cascading deletes are set up? No explicit remove from the context is done, but the records missing a mandatory owner will be removed.

I am trying to catch those deletes in a Deleted trigger, but it doesn't seem that i get there. (trying to keep an in-memory cache syncronized with the DB)

Instead, it seems like the entities are showing up in the Updated trigger with the parent navigation property set to null, but this is much more complicated to handle in a generic way. (I am doing this for a number of different entitytypes, so the code in that case would have to figure out if the navigation is in fact cascading and which field it should test for null)

Running Triggers 1.1.1 with EF Core 2.2.1

@NickStrupat NickStrupat self-assigned this Jan 25, 2019
@NickStrupat
Copy link
Owner

You have an interesting use case. Ideally the triggers library would figure out this situation and run the right triggers.

I have tried to recreate your scenario with both Sqlite and SQL Server providers and I don't see the Updated triggers being fired for the cascade-deleted entities. Could you post a small example showing the behaviour you've described?

@JorgenLindell
Copy link
Author

JorgenLindell commented Jan 27, 2019

I don't have any simple example available, the actual use is rather complex, but basically I have one recordtype (R) that implements a many to many relationship, pointing to two parent-types (A & B).
In both of these relations, the relationship is marked with cascading delete. I am using SQL-server.

The update process is simply that I update parent type A, and in the process simply replaces the navigation collection containing R records with a new collection containing those that should be kept.

In the database this ends up cleanly, the records not present in that navigation collection are removed, and any new ones are added.

In triggers, I get the "Updated" trigger for the removed records, with their parent A navigation set to null. (But with the foreign key still containing the id of the parent), but not the "Deleted" trigger.

I have currently figured out a workaround that recognizes that the parent navigation is set to null and that the relationship isn't optional, and that way I can see that the record needs to be deleted from my cache, but it is still confusing that it happens this way.

(also, the R records have a separate DbId field (indexed integer) as primary key, in case that makes any difference)

@JorgenLindell
Copy link
Author

JorgenLindell commented Jan 27, 2019

Actually, it is only the relation to that recordtype that I am updating that is cascading, but that shouldn't really make any difference

This is how it is defined:

`

public partial class OfferingEquipment
{
public OfferingEquipment() { }
public int Id { get; set; }
public int OfferingId { get; set; }
public int EquipmentTypeId { get; set; }
public virtual Offering Offering { get; set; }
public virtual EquipmentType EquipmentType { get; set; }

internal static void Configure(ModelBuilder modelBuilder)
{
modelBuilder.Entity(
entity =>
{
entity.Property(e => e.Id).ValueGeneratedOnAdd();

  entity.HasOne(d => d.Offering)
        .WithMany(p=> p.OfferingEquipments)
        .HasForeignKey(d => d.OfferingId)
        .OnDelete(DeleteBehavior.Cascade)
        .HasConstraintName("FK_Offering_OfferingEquipment");

  entity.HasOne(d => d.EquipmentType)
        .WithMany(p=>p.OfferingItems)
        .HasForeignKey(d => d.EquipmentTypeId)
        .HasConstraintName("FK_OfferingEquipment_EquipmentType");
});

}
}`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants