Thursday, May 21, 2020

Delete the foreign key data when a primary key is deleted

According to your description, I create a similar as below, it works well. I couldn't reproduce your issue on my side.

#models

 [Table("Blog")]
    public partial class Blog
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public Blog()
        {
            Posts = new HashSet<Post>();
        }

        public int BlogId { get; set; }

        [Required]
        public string Url { get; set; }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<Post> Posts { get; set; }
    }


[Table("Post")]
    public partial class Post
    {
        public int PostId { get; set; }

        public int BlogId { get; set; }

        public string Content { get; set; }

        public string Title { get; set; }

        public virtual Blog Blog { get; set; }
    }

#Dbcontext

namespace IncludeDeleteDemo
{
    using System.Data.Entity;

    public partial class BlogContext : DbContext
    {
        public BlogContext()
            : base("name=BlogContext")
        {
        }

        public virtual DbSet<Blog> Blogs { get; set; }
        public virtual DbSet<Post> Posts { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
        }
    }
}

#Usage:

using (var db = new BlogContext())
            {
                Blog blog = db.Blogs.Include("Posts")
                    .Where(i => i.BlogId == 1003).Single();

                db.Blogs.Remove(blog);
                db.SaveChanges();            
            }

In addition, please try to use Code First Cascade Delete, add the Fluent API, please modify related model name as your requirement.

modelBuilder.Entity<Course>() 
    .HasRequired(t => t.Department) 
    .WithMany(t => t.Courses) 
    .HasForeignKey(d => d.DepartmentID) 
    .WillCascadeOnDelete(true);

No comments:

Post a Comment

How to register multiple implementations of the same interface in Asp.Net Core?

 Problem: I have services that are derived from the same interface. public interface IService { } public class ServiceA : IService { ...