Using MVC3/EF CodeFirst with Repository Pattern, want to add sample data, getting error
I'm having trouble figuring out how to add sample data to an MVC3 application that uses EF Code First. I've pasted my sample code below as well as the error I'm getting. Is there a good pattern to follow for adding sample data?
Any ideas appreciated.
public ViewResult Index()
{
return View(usersRepository.GetAllUsers());
}
public ViewResult PopulateData()
{
PopulateUsersDb();
return View(usersRepository.GetAllUsers());
}
private void PopulateUsersDb()
{
Users user1 = new Users()
{
CreationDate = DateTime.Today,
Id = 1001,
FirstName = "David",
LastName = "Nadler",
PlanExpirationDate = DateTime.Now.AddMonths(6),
Username = "dnadler"
};
usersRepository.InsertOrUpdate(user1);
usersRepository.Save();
Error:
Store update, insert, or delete st开发者_高级运维atement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. Refresh ObjectStateManager entries.
Check this out.. repository pattern with ef code first. Maybe it can help you out. http://average-uffe.blogspot.com/2011/03/repository-pattern-with-ef-code-first.html
精彩评论