This can also be caused if you set the Entity state incorrectly. I kept getting the following error when starting the update database ... "The sequence contains several matching items."
For example, I created duplicate rows for each update-database command (which, of course, should not happen when sowing data), and then the next update database command will not work at all, because it found more than one match (therefore, sequence error indicating that I have multiple matching lines). This is because I overridden SaveChanges in my context file with a call to the ApplyStateChanges method ...
public override int SaveChanges() { this.ApplyStateChanges(); return base.SaveChanges(); }
I used ApplyStateChanges to ensure that when adding object graphs, the Entity Framework explicitly knows whether the object is in an added or changed state. All explanation of how I use ApplyStateChanges can be found here .
And this works fine (but beware !!) ... if you also host the database using CodeFirst migration, then the above method will cause chaos for calling AddOrUpdate () in the seed method. Therefore, before anything else, just check the DBContext file and make sure that you do not override SaveChanges above, or you will get a second copy of the duplicate data executing the update-database command, and then you won’t work at all for the third time, because for each corresponding An item has more than one row.
When it comes to this, you don’t need to configure the Id in AddOrUpdate () ..., which defeats the whole purpose of a simple and initial seed database. It works fine with something like:
context.Students.AddOrUpdate( p => p.StudentName, new Student { StudentName = "Bill Peters" }, new Student { StudentName = "Jandra Nancy" }, new Student { StudentName = "Rowan Miller" }, new Student { StudentName = "James O'Dalley" },
just AS LONG, since I am not overriding the SaveChanges method in my context file with a call to ApplyStateChanges. Hope this helps.
firecape Jul 28 '13 at 21:24 2013-07-28 21:24
source share