Delete Child record with Parent Foreign key

entity-framework entity-framework-core sql sql-server

Question

I have a one-to-many relationship with child-parent with a list of the children in the parent, I deleted the list but in the database it only deletes the parents Id,

enter image description here

Parent:

  public void Configure(EntityTypeBuilder<VehicleCategory> builder)
    {

        builder.OwnsOne(o => o.Name,
            sa =>
            {
                sa.Property("StringValue").HasColumnName("Name").IsUnicode(true).IsRequired();
                sa.Ignore("CurrentCultureText");
            });

        var navigation = builder.Metadata.FindNavigation(nameof(VehicleCategory.Successors));
        navigation.SetPropertyAccessMode(PropertyAccessMode.Field);
        builder.Property(c => c.IsActive);
        builder.Property(c => c.Sequence);
    }

Child

public void Configure(EntityTypeBuilder<VehicleSuccessorCategory> builder)
    {
        builder.HasOne<VehicleCategory>().WithMany().HasForeignKey(p => p.SuccessorId).IsRequired().OnDelete(DeleteBehavior.Cascade);
        builder.Property(e => e.Order).IsRequired();
    }

Clearing the list

 public virtual void DeleteAll()
    {
        _successors.Clear();
    }
1
0
3/9/2020 3:23:25 PM

Popular Answer

builder.HasOne<VehicleCategory>().WithMany(e => e.Successors).OnDelete(DeleteBehavior.Cascade).IsRequired();
0
3/10/2020 2:40:49 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow