I am learning ASP.NET MVC and Entity Framework.
After I learning from this web references, I think that when we want to make update process,
- we need to remove existing record first
- then insert new record.
My code:
public List<UserModel> _usrList = new List<UserModel>();
public void Update(UserModel umToUpdate)
{
foreach (UserModel um in _usrList)
{
if (um.UserName == umToUpdate.UserName)
{
_usrList.Remove(um);
_usrList.Add(umToUpdate);
break;
}
}
}
When we were doing in traditional way, we can update data without removing it.
(eg. update tablename set column = value where columnname = value)
So now I am searching for the way to make update process without removing original rows.
Is this possible in Entity Framework?
Please let me know the way.
That example is about creating views, and client side validation, sadly the server side part (IMO) is bad. Bad in the sense, that it creates confusion…
You are right, you shouldn’t do that with a real database.
But there are EF examples on the asp.net site: http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/implementing-basic-crud-functionality-with-the-entity-framework-in-asp-net-mvc-application
Answers:
Almost, that line tells EF that that record is updated, and these are the current values. Right now changes are only in the server’s memory. You actually save the changes back to the database by issuing the
db.SaveChanges();command. The rest is handled by EF. See the "Creating an Edit Page" section in the example.Nope, sadly the first example without a db is confusing. When I first saw that, I thought "WTF is that guy smoking?". Just to figure out that it is an official sample…
Don’t have to do it explicitly, because MVC’s model binding takes care of reading up all values (including the PersonID in a hidden field) from the posted data. The ID of the current record is in a html hidden field, so MVC knows which record you are updating. For Example: Views\Students\Edit.cshtml row 17:
And everything works "automagically".