Entity Framework general operation (for review) – it feels a bit wrong, code verification is required_baimanmu3398’s blog
Method 1, use Attach, and update the value of an attribute (note that not all attributes are modified) using (var context = new EFContext()) { //Method 1 var entity = context.Citys.Find(4); context.Citys.Attach(entity); entity.Name = “Zhaoqing”; context. SaveChanges(); } When an entity is marked as System.Data.Entity.EntityState.Modified, all columns will be updated (not just modified columns), which method to use depends on the occasion. The model object may not be obtained from the database using (var context = new EFContext()) { //Method 2 var model = context.Citys.Find(5); model.Name = “Chaozhou”; context.Entry(model).State = System.Data.Entity.EntityState.Modified; context. SaveChanges(); } Normal mode, update all fields using (var db = new DBModel()) { var student = db.students.FirstOrDefault(s => s.name == ” Loli”); student.age = 13; //Change Lori’s age to 13 db. SaveChanges(); } Delete in normal way using (var db = new DBModel()) { var student = db.students.FirstOrDefault(s => s.name == ” Loli”); //Find loli db.students.Remove(student); //Delete loli db. SaveChanges(); } Use sql statement in ef using (var db = new DBModel()) //Create a database context { //Execute SQL synchronously and return the number of affected rows int result = db.Database.ExecuteSqlCommand(@”CREATE TABLE `test`.`test` ( `id` INT NOT NULL, PRIMARY KEY(`id`)); “); //Using SqlParameter to pass values can…