when inserting we just do
MyDBEntities db = new MyDBEntities();
TableNameEntity o = new TableNameEntity();
o.Name = "me";
o.Age = 12;
db.TableNameEntity.AddObject(o); // Added
o = new TableNameEntity();
o.Name = "you";
o.Age = 23;
db.TableNameEntity.AddObject(o); // Added
db.SaveChanges(); // commits to db
Works fine.
But how can I do the same under an Update?
TableNameEntity o = db.TableNameEntity.Where(x=>x.id.Equals(tableId_1))
.FirstOrDefault();
o.Name = "myself";
o = db.TableNameEntity.Where(x=>x.id.Equals(tableId_2)).FirstOrDefault();
o.Name = "yourself";
db.SaveChanges(); // commits to db .. Err!!!
there is no way to keep objects as well? so we just SaveChanges
at the end?
What I'm after is to have a common place to place and hold the objects and only at the end, I commit all changes to the database.
currently in the Insert:
// Create 1nd Object
TableNameEntity o = new TableNameEntity();
// Don't Update the Database, just place it in the "basket"
db.TableNameEntity.AddObject(o);
// Create 2nd Object
o = new TableNameEntity();
// Don't Update the Database, just place it in the "basket"
db.TableNameEntity.AddObject(o);
// Now that I have all my objects done, let's 开发者_如何学JAVAcommit all to the DB
db.SaveChanges();
Is there a form to do this but just UPDATING (so we can't use AddObject
)
Look here you can use stub entities to update objects. This will allow you to attach stubs and update or insert, then save the context. Link
Note this will also save you the time of running Queries against the db to find the object to update.
精彩评论