问题描述
我有一个问题.我有一些看起来像这样的班级:
I have an issue.I have some classes that look like this:
public class Question
{
public int Id { get; set; }
[Required] [MaxLength(255)] public string Text { get; set; }
public int CriteriaId { get; set; }
public int QuestionGroupId { get; set; }
public QuestionGroup QuestionGroup { get; set; }
public virtual IList<Answer> Answers { get; set; }
public virtual Criteria Criteria { get; set; }
}
public class Answer
{
public int Id { get; set; }
[Required] [MaxLength(255)] public string Text { get; set; }
public int QuestionId { get; set; }
public Question Question { get; set; }
public virtual IList<State> States { get; set; }
}
public class Criteria
{
public int Id { get; set; }
[Required] [MaxLength(100)] public string Name { get; set; }
public virtual IList<State> States { get; set; }
public IList<Question> Questions { get; set; }
}
public class State
{
public int Id { get; set; }
public int CriteriaId { get; set; }
[Required] [MaxLength(100)] public string Name { get; set; }
public int Score { get; set; }
public virtual IList<Filter> Filters { get; set; }
public Criteria Criteria { get; set; }
}
它们之间存在多对多关系,因此我创建了此映射:
They have a many to many relationship, so I created this mapping:
modelBuilder.Entity<Answer>()
.HasMany(m => m.States)
.WithMany()
.Map(m => {
m.MapLeftKey("AnswerId");
m.MapRightKey("StateId");
m.ToTable("AnswerStates");
});
当我尝试更新数据库时,出现有关外键的错误.因此,我添加了这一行(作为临时解决方法):
When I tried to update my database I get an error about foreign keys.So I added this line (as a temporary fix):
modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>();
但是我需要级联删除.我似乎无法弄清楚为什么它不让我拥有它.尝试包括级联删除时出现的错误是:
But I need the cascading delete.I can't seem to figure out why it won't let me have it.The error I get when trying to include cascading deletes is:
我希望我提供了足够的信息.有谁知道我可以做些什么来允许级联删除(我需要能够删除 State 或 Answer ,然后从 AnswerStates中删除记录
I hope I have provided enough information. Does anyone know what I can do to allow the cascading delete (I need to be able to delete a State or an Answer and it to remove the record from AnswerStates
推荐答案
这与另一个问题()我今天在回答.只是您的情况要复杂一些.
This is pretty much the same as another question (Deleting only one entry from Many-to-Many relationship) I was answering today. Just your case is a bit more complicated.
您的情况下,多级联路径是从 Criteria
到 AnswerStates
.删除 Criteria
记录时, Criteria-> States-> AnswerStates
或 Criteria->问题->答案->答案状态
关系.
The multiple cascade path in your case is from Criteria
to AnswerStates
. When deleting a Criteria
record, the AnswerStates
records can be deleted either by Criteria->States->AnswerStates
or Criteria->Questions->Answers->AnswerStates
relationships.
解决方案始终是相同的-关闭关系 cascade delete
之一,然后手动或通过触发器处理删除.
The solution is always one and the same - turn one of the relationships cascade delete
off and handle deletion either manually or via trigger.
在这种情况下,我的建议是关闭 Criteria-> States
级联删除:
In this particular case, my suggestion is to turn Criteria->States
cascade delete off:
modelBuilder.Entity<Criteria>()
.HasMany(m => m.States)
.WithRequired(d => d.Criteria)
.HasForeignKey(d => d.CriteriaId)
.WillCascadeOnDelete(false);
并在删除条件
之前使用类似的内容:
and use something like this before deleting a Criteria
:
db.States.RemoveRange(db.States.Where(s => s.CriteriaId == criteriaId_to_be_removed));
这篇关于EF多对多级联删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!