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?
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Why am I getting a foreign key conflict from seeding?
Feb 22, 2012 01:15 AM|LINK
Show us your tables' definations in Code-First,I suspect there's something wrong with that。
thaicarrot
Contributor
5132 Points
1465 Posts
Re: Why am I getting a foreign key conflict from seeding?
Feb 22, 2012 08:03 AM|LINK
There is no reason to call base because you have override those.
protected override void Seed(SchoolContext context)
{
var students = new List<Student>
{
new Student { FirstMidName = "Carson", LastName = "Alexander", EnrollmentDate = DateTime.Parse("2005-09-01") },
new Student { FirstMidName = "Meredith", LastName = "Alonso", EnrollmentDate = DateTime.Parse("2002-09-01") },
new Student { FirstMidName = "Arturo", LastName = "Anand", EnrollmentDate = DateTime.Parse("2003-09-01") },
new Student { FirstMidName = "Gytis", LastName = "Barzdukas", EnrollmentDate = DateTime.Parse("2002-09-01") },
new Student { FirstMidName = "Yan", LastName = "Li", EnrollmentDate = DateTime.Parse("2002-09-01") },
new Student { FirstMidName = "Peggy", LastName = "Justice", EnrollmentDate = DateTime.Parse("2001-09-01") },
new Student { FirstMidName = "Laura", LastName = "Norman", EnrollmentDate = DateTime.Parse("2003-09-01") },
new Student { FirstMidName = "Nino", LastName = "Olivetto", EnrollmentDate = DateTime.Parse("2005-09-01") }
};
students.ForEach(s => context.Students.Add(s));
context.SaveChanges();
}
Weera