In the below code I am updating IndexOrder by Id using TryUpdateModel. Below update hit DB 2 times. Once retrieving current record by Id than updating single row.
I am using LINQ To EF
foreach (var i in indexArraay)
{
SubMenu existingMenu = _menu.SubSingle(Int32.Parse(i.id.ToString()));
existingMenu.IndexOrder = string.IsNullOrEmpty(i.index.ToString()) ? 0 : Int32.Parse(i.index.ToString());
TryUpdateModel(existingMenu);
_menu.Add();
}
In SQL we don’t do that like in below statement.
UPDATE table_name
SET column1=value, column2=value2,...
WHERE some_column=some_value
Am I wrong somewhere or Is there any better way to write LINQ update expression?
I am not talking about performance here.
In LINQ-to-SQL you can use
Attachfor this. The code below will updateOrdervalue for exisitin menu item with id 1.[Edit]
For LINQ-to-Entities take a look at Attaching and Detaching Objects.
The analog is ObjectSet.Attach Method.