I have a simple project with a blog format. There are users and blogs. The user id is present as a foreign key in the blog table. SQL Creation query as follows:
CREATE TABLE BlogUser (
BlogUser_ID int NOT NULL IDENTITY (1,1) CONSTRAINT PK_User PRIMARY KEY CLUSTERED,
DisplayName varchar(50) NOT NULL);
CREATE TABLE BlogPost (
Post_ID int NOT NULL IDENTITY (1,1) CONSTRAINT PK_Blog PRIMARY KEY CLUSTERED,
BlogUser_ID int NOT NULL CONSTRAINT FK_BlogPost_BlogUser REFERENCES BlogUser(BlogUser_ID),
BlogTitle varchar(30) NOT NULL,
BlogDate datetime NOT NULL,
BlogContent varchar(8000) NOT NULL);
The following are the models:
public partial class BlogPost
{
public int PostId { get; set; }
public int BlogUserId { get; set; }
public string BlogTitle { get; set; }
public DateTime BlogDate { get; set; }
public string BlogContent { get; set; }
public virtual BlogUser BlogUser { get; set; }
}
public partial class BlogUser
{
public BlogUser()
{
BlogPost = new HashSet<BlogPost>();
}
public int BlogUserId { get; set; }
public string DisplayName { get; set; }
public virtual ICollection<BlogPost> BlogPost { get; set; }
}
These are the Models in the DbContext Class:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
//Blog Post
modelBuilder.Entity<BlogPost>(entity =>
{
entity.HasKey(e => e.PostId).HasName("PK_Blog");
entity.Property(e => e.PostId).HasColumnName("Post_ID");
entity.Property(e => e.BlogContent)
.IsRequired()
.HasMaxLength(8000)
.IsUnicode(false);
entity.Property(e => e.BlogDate).HasColumnType("datetime");
entity.Property(e => e.BlogTitle)
.IsRequired()
.HasMaxLength(30)
.IsUnicode(false);
entity.Property(e => e.BlogUserId).HasColumnName("BlogUser_ID");
entity.HasOne(d => d.BlogUser)
.WithMany(p => p.BlogPost)
.HasForeignKey(d => d.BlogUserId)
.OnDelete(DeleteBehavior.Cascade) //--> What is missing here?
.HasConstraintName("FK_BlogPost_BlogUser");
});
//Blog User
modelBuilder.Entity<BlogUser>(entity =>
{
entity.Property(e => e.BlogUserId).HasColumnName("BlogUser_ID");
entity.Property(e => e.DisplayName)
.IsRequired()
.HasMaxLength(50)
.IsUnicode(false);
});
OnModelCreatingPartial(modelBuilder);
}
If I delete a user who has posted blogs, I would like to have all the blogs they posted deleted as well without having to write a script to remove their blogs one by one.
As you can see, I've placed the OnDelete(DeleteBehavior.Cascade) on the foreign key that links the two. But when I try to delete a user with blogs, I receive an error stating 'The DELETE statement conflicted with the REFERENCE constraint "FK_BlogPost_BlogUser"'.
What am I missing?
This is actually for a larger project where there are upwards of 30 dependencies for several tables. The cascade behaviour is preferred for that part of the project, so its just logical to ask .NET to Cascade instead of writing 30 separate loops for each. Ideally the cascade behaviour should be applied only to the models in question and not the project as a whole.