Entity Framework 4.1 Insert, Update Delete ASP.NET MVC3
I have a wizard where I am in updatemode. During this mode, I can actually insert, delete or update various records in the model. I am passing by reference to m CRUD methods. eg. MyMethod(ref Project project)
开发者_如何学编程I was able to update by attaching my project to the context but when I also need to Delete during the same tranaction, update and delete do not do anything, How am I supposed to handle delete? I do the following which does not work.
var FoundProjectUser = (from m in UserRoles where m.UserProfileId == member.UserProfileId select m);
if (FoundProjectUser.Count() == 0)
{
project.ProjectTeams.Remove(member);
}
ANSWER FOUND:
I found the problem. The problem is that in edit mode, the project is not attached to the context. I need to delete from the DBContext and not the project. Like this.Context.ProjectTeams.Remove(member);
.Remove will only flag the row for removal. You have to do entity.SaveChanges() to persist the changes.
精彩评论