标签:fir sop remove elb book false 执行 mod author
authors表是主表
books表是子表
默认情况下,当使用datacontext.dbset.remove 删除authors时, 与之相关的books也会一起删除掉.
using (var context = new BookStore()) { var author = context.Authors .FirstOrDefault(); context.Authors.Remove(author); context.SaveChanges(); }
不过可以使用WillCascadeOnDelete(false)来关闭级联删除
protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<Book>() .HasOptional<Author>(b => b.Author) .WithMany() .WillCascadeOnDelete(false); }
这样再执行本随笔第一个代码段就不会删除与之相关的books了.
标签:fir sop remove elb book false 执行 mod author
原文地址:https://www.cnblogs.com/nocanstillbb/p/11494474.html