MVC ---- EF高级增删改
//高级修改(创建对象) public void EditAdance(){ //创建要修改的对象 Parameter pm = new Parameter() { ParaNo = "00001", ParaName = "会员卡补偿" //修改参数名称 }; //通过上下文获取对象相关信息 //Entry获取给定实体的对象 DbEntityEntry<Parameter> entry = dbNe.Entry<Parameter>(pm); //对象的状态是没有修改 entry.State = System.Data.Entity.EntityState.Unchanged; //对象的属性值ParaName修改了 entry.Property("ParaName").IsModified = true; //会只能判断哪个对象的那个属性值修改了 dbNe.SaveChanges(); } //高级删除 public void DeleAdvance() { //先查后删除 Parameter pDe =new Parameter() { paraNo = "00002" } //通过上下文获取对象的相关信息 DbEntityEntry<Parameter> entry =dbNe.Entity<Parameter>(pDe); //对象的状态是删除 entry.State = System.Data.EntityState.Deleted; //智能判断对象的状态 dbNe.SaveChanges(); } //高级参数新增 public void AddAdvance() { //参数对象 Parameter parm = new Parameter(){ ParaNo = "00003", ParaName = "预交金初始值", ParaValue = "100", ParaRemark = "" }; DbEntityEntry<Parameter> entry = dbNe.Entry<Parameter>(parm); entry.State = System.Data.EntityState.Added; dbNe.SaveChanges();//保存至数据库 }
第二次总结方法
//修改
public IActionResult Update(int Id) { UserTable ut = dbContext.Set<UserTable>().Find(Id); ut.Name = "武器大师"; //旧写法 //var entry = dbContext.Set<UserTable>().Attach(ut); //dbContext.Entry(ut).State = EntityState.Modified; //dbContext.SaveChanges(); //新写法 dbContext.Set<UserTable>().Update(ut); dbContext.SaveChanges(); return RedirectToAction("Index"); }
//删除 public IActionResult Del(string Id) { UserTable ut = dbContext.Set<UserTable>().Find(Id); dbContext.Set<UserTable>().Remove(ut); dbContext.SaveChanges(); return RedirectToAction("Index"); }
public class EFDao<T> : IDao<T> where T : class { private EnergyIotDbContext db; public EFDao(EnergyIotDbContext _db) { db = _db; } public async Task<bool> Add(T Entity) { //老写法 //db.Entry(Entity).State = EntityState.Added; await db.Set<T>().AddAsync(Entity); return await db.SaveChangesAsync() > 0; } public async Task<bool> Delete(T Entity) { //老写法 //db.Set<T>().Attach(Entity); //db.Entry(Entity).State = EntityState.Deleted; //新写法 db.Set<T>().Remove(Entity); return await db.SaveChangesAsync() > 0; } public async Task<bool> Update(T Entity) { //老写法 //db.Set<T>().Attach(Entity); //db.Entry(Entity).State = EntityState.Modified; //新写法 db.Set<T>().Update(Entity); return await db.SaveChangesAsync() > 0; } public IEnumerable<T> GetEntities(Expression<Func<T, bool>> exp) { //var data = db.Set<T>().Find() return CompileQuery(exp); } public IEnumerable<T> GetEntitiesForPaging(int Page, int pageSize, Expression<Func<T, bool>> exp) { return CompileQuery(exp).Skip((Page - 1) * pageSize).Take(pageSize); } public T GetEntity(Expression<Func<T, bool>> exp) { return CompileQuerySingle(exp); } private IEnumerable<T> CompileQuery(Expression<Func<T, bool>> exp) { var func = EF.CompileQuery((EnergyIotDbContext context, Expression<Func<T, bool>> exps) => context.Set<T>().Where(exp)); return func(db, exp); } private T CompileQuerySingle(Expression<Func<T, bool>> exp) { var func = EF.CompileQuery((EnergyIotDbContext context, Expression<Func<T, bool>> exps) => context.Set<T>().FirstOrDefault(exp)); return func(db, exp); } }