如果将 Fruit 对象传递给另一个方法,您可以:
将对上下文对象的引用与 Fruit 一起传递并调用 SaveChanges()
在下级方法中对 Fruit 对象进行编辑,并在调用方法中调用 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();