When trying to update a List in a Model got an error: Violation of PRIMARY KEY constraint
When trying to update a List in a Model got an error: Violation of PRIMARY KEY constraint
我正在开发一个用于考试和其他内容的应用程序...
在我的应用程序中,我添加了问题,然后我可以将它添加到测试中。
我也在使用 Code First。
这是我的问题模型:
public class Question
{
public Question() {
this.Tests = new HashSet<Test>();
}
public int QuestionId { get; set; }
[Display(Name = "Descripción")]
public string QuestionDescription { get; set; }
[Display(Name = "Competencia")]
public int ProfiencyId { get; set; }
public virtual Proficiency Profiency { get; set; }
public virtual ICollection<Test> Tests { get; set; }
}
我的测试模型:
public class Test
{
public Test() {
this.Questions = new HashSet<Question>();
}
public int TestId { get; set; }
[Display(Name = "Descripción")]
public string TestDescription { get; set; }
[Display(Name = "Tipo de evaluación")]
public EVALUATE_TO EvaluateTo { get; set; }
public ICollection<Question> Questions { get; set; }
}
我使用 Fluent API 来处理多对多关系。
modelBuilder.Entity<Question>()
.HasMany(question => question.Tests)
.WithMany(test => test.Questions)
.Map(ma =>
{
ma.MapLeftKey("QuestionId");
ma.MapRightKey("TestId");
ma.ToTable("QuestionTest");
});
我正在用这种方法更新测试问题。
private void UpdateTestQuestions(string[] selectedQuestions, Test testToUpdate)
{
if (selectedQuestions == null)
{
testToUpdate.Questions = new List<Question>();
return;
}
var selectedQuestionsHS = new HashSet<string>(selectedQuestions);
var testQuestions = new HashSet<int>
(testToUpdate.Questions.Select(c => c.QuestionId));
foreach (var question in ApplicationDbContext.Questions)
{
if (selectedQuestionsHS.Contains(question.QuestionId.ToString()))
{
if (!testQuestions.Contains(question.QuestionId))
{
if (!testToUpdate.Questions.Contains(question)) {
testToUpdate.Questions.Add(question);
}
}
}
else
{
if (testQuestions.Contains(question.QuestionId))
{
testToUpdate.Questions.Remove(question);
}
}
}
}
当我尝试保存数据库时,应用程序崩溃了,我收到如下错误:
违反 PRIMARY KEY 约束 'PK_dbo.QuestionTest'。无法在对象 'dbo.QuestionTest' 中插入重复键。重复键值为 (1, 1).
我关注的是 Microsoft 官方文档:
Microsoft Documentation MVC 5 Updating
他们说我们可以通过更改他的成员并保存来修改列表,但是我得到一个错误...
有人知道为什么吗?
谢谢阅读!希望大家能帮帮我。
经过所有的研究、阅读和尝试很多事情...
我刚刚看到 mi List 不是虚拟的,所以这就是问题所在......
如果有人遭受同样的事情,我只是回答...
我正在开发一个用于考试和其他内容的应用程序... 在我的应用程序中,我添加了问题,然后我可以将它添加到测试中。 我也在使用 Code First。
这是我的问题模型:
public class Question
{
public Question() {
this.Tests = new HashSet<Test>();
}
public int QuestionId { get; set; }
[Display(Name = "Descripción")]
public string QuestionDescription { get; set; }
[Display(Name = "Competencia")]
public int ProfiencyId { get; set; }
public virtual Proficiency Profiency { get; set; }
public virtual ICollection<Test> Tests { get; set; }
}
我的测试模型:
public class Test
{
public Test() {
this.Questions = new HashSet<Question>();
}
public int TestId { get; set; }
[Display(Name = "Descripción")]
public string TestDescription { get; set; }
[Display(Name = "Tipo de evaluación")]
public EVALUATE_TO EvaluateTo { get; set; }
public ICollection<Question> Questions { get; set; }
}
我使用 Fluent API 来处理多对多关系。
modelBuilder.Entity<Question>()
.HasMany(question => question.Tests)
.WithMany(test => test.Questions)
.Map(ma =>
{
ma.MapLeftKey("QuestionId");
ma.MapRightKey("TestId");
ma.ToTable("QuestionTest");
});
我正在用这种方法更新测试问题。
private void UpdateTestQuestions(string[] selectedQuestions, Test testToUpdate)
{
if (selectedQuestions == null)
{
testToUpdate.Questions = new List<Question>();
return;
}
var selectedQuestionsHS = new HashSet<string>(selectedQuestions);
var testQuestions = new HashSet<int>
(testToUpdate.Questions.Select(c => c.QuestionId));
foreach (var question in ApplicationDbContext.Questions)
{
if (selectedQuestionsHS.Contains(question.QuestionId.ToString()))
{
if (!testQuestions.Contains(question.QuestionId))
{
if (!testToUpdate.Questions.Contains(question)) {
testToUpdate.Questions.Add(question);
}
}
}
else
{
if (testQuestions.Contains(question.QuestionId))
{
testToUpdate.Questions.Remove(question);
}
}
}
}
当我尝试保存数据库时,应用程序崩溃了,我收到如下错误:
违反 PRIMARY KEY 约束 'PK_dbo.QuestionTest'。无法在对象 'dbo.QuestionTest' 中插入重复键。重复键值为 (1, 1).
我关注的是 Microsoft 官方文档: Microsoft Documentation MVC 5 Updating
他们说我们可以通过更改他的成员并保存来修改列表,但是我得到一个错误... 有人知道为什么吗? 谢谢阅读!希望大家能帮帮我。
经过所有的研究、阅读和尝试很多事情... 我刚刚看到 mi List 不是虚拟的,所以这就是问题所在...... 如果有人遭受同样的事情,我只是回答...