Entity FrameWork 增删改查
Entity Framework (又称ADO.NET Entity Framework) 是微软以 ADO.NET 为基础所发展出来的对象关系对应(O/R Mapping) 解决方案
*新增
方法一:
//实例化添加的对象
Employees employees = new Employees { FirstName="zhou",LastName="quan", Title="Manager"};
#region ef添加数据 using (var db = new NorthwindEntities()) { db.Employees.Add(employees); db.SaveChanges(); } #endregion
方法二:
//添加引用 using System.Data.Entity.Infrastructure; //实例化添加的对象 Employees employees = new Employees { FirstName="zhou",LastName="quan", Title="Manager"}; #region ef添加数据 using (var db = new NorthwindEntities()) { DbEntityEntry<Employees> enty = db.Entry<Employees>(employees); enty.State = System.Data.Entity.EntityState.Added; db.SaveChanges(); }
#endregion
*修改
方法一:
#region ef修改数据(官方推荐,先查询,在修改) using (var db = new NorthwindEntities()) { Employees employees = db.Employees.Where(r => r.EmployeeID == 1).FirstOrDefault(); if (employees != null) { employees.FirstName = "zhou"; employees.LastName = "quan"; db.SaveChanges(); } } #endregion
方法二:
//添加引用
using System.Data.Entity.Infrastructure;
#region ef修改数据(修改整个实体,所有字段都会修改,其他没有值的都会为空NULL) /定义修改的数据 Employees employees = new Employees { EmployeeID = 1, FirstName = "zhou", LastName = "quan" }; using (var db = new NorthwindEntities()) { DbEntityEntry<Employees> entry = db.Entry<Employees>(employees); entry.State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); } #endregion
注意:上面这个方法会修改整个实体所有字段的数据,没有初始化值的字段会默认给空
上面的方法一可以与方法二结合,这个代码就略了
例如sql如下:
exec sp_executesql N'UPDATE [dbo].[Employees]
SET [LastName] = @0, [FirstName] = @1, [Title] = NULL, [TitleOfCourtesy] = NULL, [BirthDate] = NULL, [HireDate] = NULL, [Address] = NULL, [City] = NULL, [Region] = NULL, [PostalCode] = NULL, [Country] = NULL, [HomePhone] = NULL, [Extension] = NULL, [Photo] = NULL, [Notes] = NULL, [ReportsTo] = NULL, [PhotoPath] = NULL
WHERE ([EmployeeID] = @2)
',N'@0 nvarchar(20),@1 nvarchar(10),@2 int',@0=N'quan',@1=N'zhou',@2=1
方法三:
//添加引用 using System.Data.Entity.Infrastructure;
#region ef修改数据 //定义修改的数据 Employees employees = new Employees { EmployeeID = 1, FirstName = "zhou", LastName = "quan" }; using (var db = new NorthwindEntities()) { //将对象加入EF容器,并获取当前实体对象的状态管理对象 DbEntityEntry<Employees> entry = db.Entry<Employees>(employees); //设置该对象为被修改为 entry.State = System.Data.Entity.EntityState.Unchanged; //设置属性是否修改 entry.Property("FirstName").IsModified = true; entry.Property("LastName").IsModified = true; db.SaveChanges(); }
#endregion
*删除
方法一:
//添加引用 using System.Data.Entity.Infrastructure;
#region ef删除数据 //定义数据 Employees employees = new Employees { EmployeeID = 1}; using (var db = new NorthwindEntities()) { DbEntityEntry<Employees> entry = db.Entry<Employees>(employees); entry.State = System.Data.Entity.EntityState.Deleted; db.SaveChanges(); } #endregion
方法二:
#region ef删除数据 //定义数据 Employees employees = new Employees { EmployeeID = 1}; using (var db = new NorthwindEntities()) { //附加要删除的对象到ef db.Employees.Attach(employees); //标记为删除--注意:标记为删除状态 db.Employees.Remove(employees); db.SaveChanges(); } #endregion
方法三(先查询,再删除):
#region ef删除数据 using (var db = new NorthwindEntities()) { var employees = db.Employees.Where(r => r.EmployeeID == 1).FirstOrDefault(); if (employees!=null) { db.Employees.Remove(employees); db.SaveChanges(); } } #endregion
*查询
简单查询:
#region ef查询数据 using (var db = new NorthwindEntities()) { //Linq查询 var result = from p in db.Employees where p.City=="上海"&&p.Title.Contains("上海") orderby p.EmployeeID ascending,p.FirstName descending select new { Name=p.FirstName,Address=p.Address}; //方法查询 var _result = db.Employees.Where(r => r.City == "上海" && r.Title.Contains("上海")).OrderBy(r => r.EmployeeID).OrderByDescending(r => r.FirstName).Select(r => new { Name = r.FirstName, Address = r.Address }); } #endregion