Hi, I have nine tables in my database that I’m attempting to seed. As long as I load just one record for each table everything works fine. When I try
seeding each table with more than one record, I get an error message if I query the database.
Here’s the error message:
Unable to determine the principal end of the 'SchoolIn.Models.CourseProgress_Parent' relationship. Multiple added entities may have the same primary key.
Here are 2 of the 9 tables in my initializer:
public class SchoolInDbInitializer: DropCreateDatabaseAlways<SchoolInDB>
protected override void Seed(SchoolInDB context)
{
context.Parents.Add(new Parent { Name = "Mary Joe", Phone = "949-999-987", Notes = "Speak only to dad" });
context.Parents.Add(new Parent { Name = "Mary Joe", Phone = "949-999-987", Notes = "Speak only to dad" });
AlexanderBla...
Member
325 Points
309 Posts
Why am I getting a foreign key conflict from seeding?
Feb 20, 2012 07:34 PM|LINK
Hi, I have nine tables in my database that I’m attempting to seed. As long as I load just one record for each table everything works fine. When I try seeding each table with more than one record, I get an error message if I query the database.
Here’s the error message:
Unable to determine the principal end of the 'SchoolIn.Models.CourseProgress_Parent' relationship. Multiple added entities may have the same primary key.
Here are 2 of the 9 tables in my initializer:
public class SchoolInDbInitializer: DropCreateDatabaseAlways<SchoolInDB>
protected override void Seed(SchoolInDB context)
{
context.Parents.Add(new Parent { Name = "Mary Joe", Phone = "949-999-987", Notes = "Speak only to dad" });
context.Parents.Add(new Parent { Name = "Mary Joe", Phone = "949-999-987", Notes = "Speak only to dad" });
context.Courses.Add (new Course{Name="Algebra 1", LearningResource ="PearsonEtext",PrerequisiteGrade ="Pre Algebra B", TotalCredits =5});
context.Courses.Add(new Course { Name = "Algebra 1", LearningResource = "PearsonEtext", PrerequisiteGrade = "Pre Algebra B", TotalCredits = 5 });
base.Seed(context);
}
Here’s the action that queries the database:
public ActionResult Search(String q)
{
if (q == "")
else
ViewBag.searchterm = q
var courseprogresses = db.CourseProgresses.Include("Teacher").Where(a => a.Teacher.Name.Contains(q) || q == null).Take(10);
return View(courseprogresses);
}
Does it have anything to do with not specifying primary keys in the initializer?