OnDelete in EntityTypeBuilder ASP.NET Core 2 Entity Framework

asp.net-core-2.0 c# entity-framework-core


I use the following modelbuilder to setup my relationships on my database.

In my datacontext I have;

protected override void OnModelCreating(ModelBuilder modelBuilder)
    modelBuilder.HasDefaultSchema(schema: DbGlobals.SchemaName);

    modelBuilder.AddConfiguration<Address>(new AddressConfiguration());
    /*reduced for brevity*/


where in the AddressConfiguration() i have the following;

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace Data.EF.Configuration
    internal class AddressConfiguration : DbEntityConfiguration<Address>
        public override void Configure(EntityTypeBuilder<Address> entity)
            entity.HasKey(x => x.Id);
            entity.Property(x => x.Latitude).HasColumnType($"decimal(9,6)").IsRequired();
            entity.Property(x => x.Longitude).HasColumnType($"decimal(9,6)").IsRequired();
            //I have tried the following but it says doesnt exist

Now my address model has a List<Contact> Contacts { get; set; }. How can I configure the model. to delete cascade on when the Address is deleted?

I found the following link;

Delete Cascade in EF Core

Which is detailing an OnDelete method, however this doesnt seem to be present on the EntityTypeBuilder<T>?

Can anyone tell me what I am doing wrong here please?

5/31/2018 2:29:18 PM

Accepted Answer

OnDelete specifies how to configure delete operation when applied to delete the dependent entities in the relationship when the principal is deleted or the relationship is severed.

It is available on ReferenceCollectionBuilder|ReferenceReferenceBuilder objects. Read about here.


You should define the relationship as

        .HasMany(a => a.Contacts)
        .WithOne(c => c.Address)
10/17/2019 1:13:38 AM

Related Questions


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