如果水果對象被傳遞到另一個方法,您可以:
與水果一起傳遞給上下文對象的引用,並調用的SaveChanges()
讓您編輯在下層方法水果對象,然後調用的SaveChanges()的調用方法(你可以檢查,看它是否已被修改,如果你想避免不必要的DB調用。)
郵編:
//Change the name
FruitEntities fe = new FruitEntities();
Fruit f = fe.Friuts.First();
f.FruitName = "NewName";
fe.SaveChanges();
//Get a fruit by ID and change the status
//Statuses will need to be included as an Entity in your model with an association to Fruits
int Id = 2;
int newStatusID = 0;
FruitEntities fe = new FruitEntities();
Fruit f = (from x in fe.Fruits
where x.FruitID == Id
select x).First();
Status s = (from y in fe.Statuses
where y.StatusID = newStatusID
select y).First();
f.Status = s;
fe.SaveChanges();
我也意識到:你可以從現有的EntityKey值構建前兩個參數,所以你不必硬編碼它們:新的EntityKey(Fruit.FruitIDReference.EntityKey.EntityContainerName + 「。」+ Fruit.FruitIDReference.EntityKey.EntitySetName,Fruit.FruitIDReference.EntityKey.EntityKeyValues [0] .Key,0) – 2009-05-12 21:04:27
我會試試看,謝謝你的跟進 – 2009-05-13 02:46:07