Entity Framework 4.0 Beta: Updating Reference Keys
LINQ
1
Posts
1
Posters
0
Views
1
Watching
-
Hi! I created the following model: EF Model[^] Now I have ASP.NET MVC and want to edit an existing product. When I save the updated product only product proprties are saved instead of the other references too. So the foreign keys of product didn't change. Here is my Code to update the product:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(Product product, Unit unit, Packing packing, Shelf shelf, Bestbefore bestbefore )
{
product.BestbeforeReference.EntityKey =
new System.Data.EntityKey("WebDBEntities.Bestbefore", "BestbeforeID", bestbefore.BestbeforeID);product.PackingReference.EntityKey = new System.Data.EntityKey("WebDBEntities.Packing", "PackingID", packing.PackingID); product.ShelfReference.EntityKey = new System.Data.EntityKey("WebDBEntities.Shelf", "ShelfID", shelf.ShelfID); product.UnitReference.EntityKey = new System.Data.EntityKey("WebDBEntities.Unit", "UnitID", unit.UnitID); if (ModelState.IsValid) { \_db.AddToProduct(product); \_db.SaveChanges(); return RedirectToAction("ProductList"); } return RedirectToAction("ProductList"); }
Can someone explain me how to udate the foreign keys? :confused: Thanks in advance!