This is my model builder right now, but when i delete user it sets recipes userid to null.

  protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.Entity<ApplicationUser>().HasMany(e => e.Recipes).WithOne(e => e.ApplicationUser)
                .OnDelete(DeleteBehavior.Cascade);

            modelBuilder.Entity<Recipe>().HasOne(e => e.ApplicationUser).WithMany(e=>e.Recipes)

        }
1

There are 1 best solutions below

0
On

I think you've overstated the mapping, you only need to map one entity to the other.

Try to use this one first;

modelBuilder.Entity<ApplicationUser>().HasMany(e => e.Recipes).OnDelete(DeleteBehavior.Cascade);

modelBuilder.Entity<Recipe>().HasOne(e => e.ApplicationUser);

If the above doesn't work, try this one alone;

modelBuilder.Entity<ApplicationUser>().HasMany(e => e.Recipes).OnDelete(DeleteBehavior.Cascade);