Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Entity framework 实体框架代码前5个级联删除多对多表错误_Entity Framework_Ef Code First_Code First_Entity Framework 5 - Fatal编程技术网

Entity framework 实体框架代码前5个级联删除多对多表错误

Entity framework 实体框架代码前5个级联删除多对多表错误,entity-framework,ef-code-first,code-first,entity-framework-5,Entity Framework,Ef Code First,Code First,Entity Framework 5,我有这个型号 public class State { public State() { this.Promotions = new List<Promotion>(); this.Branches = new List<Branch>(); this.Stores = new List<Store>(); } public int Id { get; set; } pu

我有这个型号

public class State
{
    public State()
    {
        this.Promotions = new List<Promotion>();
        this.Branches = new List<Branch>();
        this.Stores = new List<Store>();
    }

    public int Id { get; set; }
    public string Description { get; set; }

    public virtual ICollection<Promotion> Promotions { get; set; }
    public virtual ICollection<Store> Stores { get; set; }
    public virtual ICollection<Branch> Branches { get; set; }
}

public class Store
{
    public Store()
    {
        this.Promotions = new List<Promotion>();
        this.Branches = new List<Branch>();
    }

    public int Id { get; set; }
    public string Name { get; set; }

    public virtual ICollection<Promotion> Promotions { get; set; }
    public virtual ICollection<Branch> Branches { get; set; }

    public int StateId { get; set; } // Foreign key
    public virtual State State { get; set; } // Navigation Property
}

public class Branch
{
    public Branch()
    {
        this.Promotions = new List<Promotion>();
    }

    public int Id { get; set; }
    public string Name { get; set; }
    public int StoreId { get; set; } // Foreign key
    public int StateId { get; set; } // Foreign key

    public virtual Store Store { get; set; } // Navigation Property
    public virtual State State { get; set; } // Navigation Property

    public virtual ICollection<Promotion> Promotions { get; set; }
}

    public class Promotion
{
    public Promotion()
    {
        this.Stores = new List<Store>();
        this.Branches = new List<Branch>();
        this.Productos = new List<Producto>();
    }

    public int Id { get; set; }
    public string Name { get; set; }
    public int StateId { get; set; }

    public virtual ICollection<Store> Stores { get; set; }
    public virtual ICollection<Branch> Branches { get; set; }
    public virtual ICollection<Product> Products { get; set; }

    public virtual State State { get; set; }

}
公共类状态
{
公共国家()
{
this.Promotions=新列表();
this.branchs=新列表();
this.Stores=新列表();
}
公共int Id{get;set;}
公共字符串说明{get;set;}
公共虚拟ICollection促销{get;set;}
公共虚拟ICollection存储{get;set;}
公共虚拟ICollection分支{get;set;}
}
公共类商店
{
公共商店()
{
this.Promotions=新列表();
this.branchs=新列表();
}
公共int Id{get;set;}
公共字符串名称{get;set;}
公共虚拟ICollection促销{get;set;}
公共虚拟ICollection分支{get;set;}
public int StateId{get;set;}//外键
公共虚拟状态状态{get;set;}//导航属性
}
公营部门
{
公共部门()
{
this.Promotions=新列表();
}
公共int Id{get;set;}
公共字符串名称{get;set;}
public int StoreId{get;set;}//外键
public int StateId{get;set;}//外键
公共虚拟存储{get;set;}//导航属性
公共虚拟状态状态{get;set;}//导航属性
公共虚拟ICollection促销{get;set;}
}
公课推广
{
公共宣传()
{
this.Stores=新列表();
this.branchs=新列表();
this.Productos=新列表();
}
公共int Id{get;set;}
公共字符串名称{get;set;}
public int StateId{get;set;}
公共虚拟ICollection存储{get;set;}
公共虚拟ICollection分支{get;set;}
公共虚拟ICollection产品{get;set;}
公共虚拟状态状态{get;set;}
}
在我的背景下:

// State
modelBuilder.Entity<State>()
    .HasMany(p => p.Promotions)
    .WithRequired(e => e.State)
    .WillCascadeOnDelete(false);

modelBuilder.Entity<State>()
    .HasMany(s => s.Branches)
    .WithRequired(e => e.State)
    .WillCascadeOnDelete(false);

modelBuilder.Entity<State>()
   .HasMany(e => e.Stores)
   .WithRequired(e => e.State)
   .WillCascadeOnDelete(true);

 // Store
modelBuilder.Entity<Store>()
    .HasMany(b => b.Branches)
    .WithRequired(s => s.Store)
    .WillCascadeOnDelete(true);

// Many to many
modelBuilder.Entity<Store>().
  HasMany(p => p.Promotions).
  WithMany(s => s.Stores).
  Map(
   m =>
   {
       m.MapLeftKey("StoreId");
       m.MapRightKey("PromotionId");
       m.ToTable("Store_Promotion");
   });

modelBuilder.Entity<Promotion>().
 HasMany(e => e.Products).
 WithMany(p => p.Promotiones).
 Map(
  m =>
  {
      m.MapLeftKey("PromotionId");
      m.MapRightKey("ProductoId");
      m.ToTable("Promotion_Producto");
  });

modelBuilder.Entity<Branch>().
 HasMany(p => p.Promotiones).
 WithMany(b => b.Branches).
 Map(
  m =>
  {
      m.MapLeftKey("BranchId");
      m.MapRightKey("PromotionId");
      m.ToTable("Branch_Promotion");
  });
//状态
modelBuilder.Entity()
.HasMany(p=>p.Promotions)
.WithRequired(e=>e.State)
.WillCascadeOnDelete(假);
modelBuilder.Entity()
.HasMany(s=>s分支)
.WithRequired(e=>e.State)
.WillCascadeOnDelete(假);
modelBuilder.Entity()
.HasMany(e=>e.Stores)
.WithRequired(e=>e.State)
.WillCascadeOnDelete(真);
//贮藏
modelBuilder.Entity()
.HasMany(b=>b.branchs)
.WithRequired(s=>s.Store)
.WillCascadeOnDelete(真);
//多对多
modelBuilder.Entity()。
有很多(p=>p.Promotions)。
有很多(s=>s.Stores)。
地图(
m=>
{
m、 MapLeftKey(“存储ID”);
m、 MapRightKey(“PromotionId”);
m、 ToTable(“商店促销”);
});
modelBuilder.Entity()。
有很多(e=>e.Products)。
有很多(p=>p.promotions)。
地图(
m=>
{
m、 MapLeftKey(“PromotionId”);
m、 MapRightKey(“ProductoId”);
m、 ToTable(“促销产品”);
});
modelBuilder.Entity()。
有很多(p=>p.promotions)。
有许多(b=>b分支)。
地图(
m=>
{
m、 MapLeftKey(“BranchId”);
m、 MapRightKey(“PromotionId”);
m、 ToTable(“分支机构推广”);
});
现在,如果我打开多个WillCascadeOnDelete状态(fluent映射中的前三个),我会得到错误

测试方法Proj.Data.Tests.UnitTest1.TestPromotion引发异常:
System.Data.SqlClient.SqlException:在表“Branch”上引入外键约束“FK_dbo.Branch_dbo.Store_StoreId”可能会导致循环或多个级联路径。指定“在删除时不执行操作”或“在更新时不执行操作”,或修改其他外键约束。 无法创建约束。请参阅前面的错误

我知道,我从朱莉·勒曼的书中读到:

某些数据库(包括SQL Server)不支持指定 指向同一个表的级联删除

这是因为多对多关系表具有来自两个相关表的级联删除


所以,我的问题是:这里唯一的选择是关闭父表上的级联删除,并手动处理关系表上的删除?EntityFramework5中没有解决这个问题的方法吗?

好的,我理解这个问题。不是要有多对多的关系,问题是

State -> Promotion -> PromotionStore
State -> Branch -> BranchPromotion
State -> Store -> StorePromotion
然后存储、分支和存储将FK设置为状态。因此,如果我删除一个状态升级存储,可以通过第1个和第3个可能性到达

我最后做的是关闭状态的级联删除并手动删除相关记录,如下所示:

public override void Delete(State state)
{
   DbContext.Entry(state).Collection(x => x.Promotions).Load();
   DbContext.Entry(state).Collection(x => x.Stores).Load();
   DbContext.Entry(state).Collection(x => x.Branches).Load();

   var associatedPromotions = state.Promotions.Where(p => p.StateId == state.Id);
   associatedPromotions.ToList().ForEach(r => DbContext.Set<Promotion>().Remove(r));

   var associatedStores = state.Stores.Where(e => e.StateId == state.Id);
   associatedStores.ToList().ForEach(e => DbContext.Set<Store>().Remove(e));

   var associatedBranches = state.Branches.Where(s => s.StateId == state.Id);
   associatedBranches.ToList().ForEach(s => DbContext.Set<Branch>().Remove(s));

   base.Delete(state);
}
公共覆盖无效删除(状态)
{
DbContext.Entry(state).Collection(x=>x.Promotions.Load();
DbContext.Entry(state).Collection(x=>x.Stores.Load();
DbContext.Entry(state).Collection(x=>x.branchs.Load();
var associatedPromotions=state.Promotions.Where(p=>p.StateId==state.Id);
associatedPromotions.ToList().ForEach(r=>DbContext.Set().Remove(r));
var associatedStores=state.Stores.Where(e=>e.StateId==state.Id);
ForEach(e=>DbContext.Set().Remove(e));
var AssociatedBranchs=state.branchs.Where(s=>s.StateId==state.Id);
AssociatedBranchs.ToList().ForEach(s=>DbContext.Set().Remove(s));
删除(状态);
}

覆盖删除函数属于哪个类?它是自定义实现吗?如何在从数据库中删除对象时自动调用删除函数?