1024programmer Blog Entity Framework general operation (for review) – it feels a bit wrong, code verification is required_baimanmu3398’s blog

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 avoid SQL injection
     var p_name = new SqlParameter(  "@name", "Loli");
     var p_age = new SqlParameter("@age", 13);

     //If you are using a MySql database, you need SqlParameter replace it with MySqlParameter
     //var p_name = new MySqlParameter("@name", "Lori")  ;
     //var p_age = new MySqlParameter("@age", 13);

     //Change student age
     result = db.Database.ExecuteSqlCommand(@"UPDATE `test`.`student`
                                            SET `age` = @age
                                            WHERE `name` = @name;", p_age, p_name);

     //Execute SQL asynchronously and return the number of affected rows
     Task<int> result2 = db.Database.ExecuteSqlCommandAsync("DROP TABLE `test`.`test`;"  );
 }

using (var db = new DBModel()) //Create a database context
 {
     //Query the information of a student named Lori and modify her age
     student result1 = db.students.SqlQuery("SELECT * FROM test.student WHERE name = 'Lori  '").FirstOrDefault();
     result1.age = 13; //The data queried by .SqlQuery under the entity collection can be saved to the database after modification
     student result2 = db.Database.SqlQuery("SELECT * FROM test.student WHERE name =  'Wangcai'").FirstOrDefault();
     result2.age = 21; //Because it is queried by .Database.SqlQuery, the modification here will not be saved to the database
     //If you want the data found under .Database.SqlQuery to be modified  Save to database
     student result3 = db.Database.SqlQuery("SELECT * FROM test.student WHERE name =  'Xiaoming'").FirstOrDefault();
     result3.age = 36;
     db.Entry(result3).State = System.Data.Entity.EntityState.Modified; //Notify the data context that this record has also been modified
  db. SaveChanges();
 }

Reprinted at: https://www.cnblogs.com/KQNLL/p/9420904.html

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/entity-framework-general-operation-for-review-it-feels-a-bit-wrong-code-verification-is-required_baimanmu3398s-blog/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索