I follow the Sports Store example in the Pro ASP.NET MVC Framework and I get a LINQ-related exception that I cannot understand. The full code is available through the website, but here is a snippet to convey the problem.
private Table<Product> productsTable;
public void SaveProduct(Product product) {
if (product.ProductID == 0)
productsTable.InsertOnSubmit(product);
else {
productsTable.Attach(product);
productsTable.Context.Refresh(RefreshMode.KeepCurrentValues, product);
}
productsTable.Context.SubmitChanges();
}
In the user interface, I update an existing product and click save, and the controller processes the message by calling SaveProduct (product) - where the product is passed through the parameter. A DuplicateKeyException is thrown at joining a product. In the debugger, the product parameter is initialized and has identifier 2.
I do not expect an exact answer, but I hope someone can give me some clues as to where I can look to solve this problem.
. , , .
public void SaveProduct(Product product) {
if (product.ProductID == 0)
productsTable.InsertOnSubmit(product);
else {
Product p2 = productsTable.Single(em => em.ProductID == product.ProductID);
p2.Name = product.Name;
p2.Description = product.Description;
p2.Price = product.Price;
p2.Category = product.Category;
}
productsTable.Context.SubmitChanges();
}