EF4.1~4.3 Code First模式级联删除是默认打开的,在同一个实体多次引用另一个实体时,需要单独设置关闭某个外键关系下的级联删除,需要写Fluent API代码,而且级联删除有时会造成麻烦。干脆整个关了吧:
using System.Data.Entity.ModelConfiguration.Conventions;
public class MyDbContext : DbContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>();
}
}