var product = (from p in dataContext.Products
where p.ProductID == 1
select p).Single();
product.Name = "New Product Name";
dataContext.SubmitChanges();
Using LINQ with MVC
The following code using the Attach method on the Linq entity will throw an exception as per Stephen Walther's excellent article Stephen Walther blog archive. This is a concurrency issue and can be remedied in 2 ways as per Stephen's article.
public ActionResult Edit(DxDataBook editedPart)
{
try
{
var originalPart = (from p in dc.DxDataBooks
where p.P_D_Num == editedPart.P_D_Num
select p).First();
if (!ModelState.IsValid)
{
return View(originalPart);
}
dc.DxDataBooks.Attach(editedPart,true);
dc.SubmitChanges();
return RedirectToAction("Index");
}
catch
{
return View();
}
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment