EF Core Cascade Delete

Viewed 305

Context: Visual Studio, Blazor .NET 5, Azure SQL Server

I have an entity with two FK. If I delete a record in one the FK tables I get the typical Referencing error. In https://docs.microsoft.com/en-us/ef/core/saving/cascade-delete it seems to say the Cascade Delete is the default. When I generate a new Migration (add-migration cascade) there is no

.OnDelete(DeleteBehavior.Cascade)
.IsRequired();

... attached to the FK properties of the entity in question in the Migration documents generated.

  1. Is there an Attribute that can be applied to the FK property in the entity class?

  2. Is there some other way of doing this in code?

  3. How do I modify the migration documentation to entrench th?

1 Answers

This is what I ended up doing .. I did it in code

    public async Task DeleteRound(int Id)
   {
       var round = _context.Rounds.Where(e => e.Id==Id).Include(e => e.Activitys).First();
       _context.Rounds.Remove(round);
       await _context.SaveChangesAsync();
   }

I had to add the following to the Round Class:

   public IList<Activity> Activitys { get; } = new List<Activity>();

No further code required. No need to populate the list as above. It seems that EF is doing it seamlessly. No need to annotate the Migration files.

Related