Bulk Update (Directly delete and auto commit)
db.People.Where(it => it.DeptId =="101").Update(it => new People { Salary = it.Salary * 1.1 });
Bulk Delete (Directly delete and auto commit)
ex:
db.People.Where(it => it.DepId =="101").Delete(); // where db the the DbContext
EF update without select (Single Update)
var newPeople = new People { PeopleId = "0001" }; // where PeopleId is the key
db.People.Attach(newPeople);
newPeople.{attribute1} = {new value1}; // assume there are 8 attributes in People object
newPeople.{attribute4} = {new value4};
newPeople.{attribute6} = {new value6};
db.SaveChanges();
after db.SaveChages(); sql will be generated like this update people set attribute1 = newValue1, attribute4 = newValue4, attribute6 = newValue6 where PeopleId = '0001';
system only update the changed items
in another case (ref to this article https://msdn.microsoft.com/en-us/data/jj592676.aspx)
var newPeople = new People { PeopleId = "0001" }; // where PeopleId is the key
db.Entry(newPoeple).State = EntityState.Modified;
newPeople.{attribute1} = {new value1}; // assume there are 8 attributes in People object
newPeople.{attribute4} = {new value4};
newPeople.{attribute6} = {new value6};
db.SaveChanges();
When you change the state to Modified all the properties of the entity will be marked as modified and all the property values will be sent to the database when SaveChanges is called. That means all attibutes in this entity will be populated to the database and the null value will be the default if you do not specify in above codes.
EF delete without select (Single Delete)
var newPeople = new People { PeopleId = "0001" }; // where PeopleId is the key
db.Entry(newPoeple).State = EntityState.Deleted;
db.SaveChanges();
or
db.Entry(new People { PeopleId = "0001"} ).State = EntityState.Deleted;
db.SaveChanges();
沒有留言:
張貼留言