如果水果对象被传递到另一个方法,您可以:
与水果一起传递给上下文对象的引用,并调用的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