1 public T AddEntity(T entity) 2 { 3 //EF4.0的写法 4 添加实体 5 //db.CreateObjectSet<T>().AddObject(entity); 6 //EF5.0的写法 7 db.Entry<T>(entity).State = EntityState.Added; 8 //下面的写法统一 9 db.SaveChanges(); 10 return entity; 11 } 12 public bool UpdateEntity(T entity) 13 { 14 //EF4.0的写法 15 //db.CreateObjectSet<T>().Addach(entity); 16 //db.ObjectStateManager.ChangeObjectState(entity, EntityState.Modified); 17 //EF5.0的写法 18 db.Set<T>().Attach(entity); 19 db.Entry<T>(entity).State = EntityState.Modified; 20 return db.SaveChanges() > 0; 21 } 22 public bool DeleteEntity(T entity) 23 { 24 //EF4.0的写法 25 //db.CreateObjectSet<T>().Addach(entity); 26 //db.ObjectStateManager.ChangeObjectState(entity, EntityState.Deleted); 27 //EF5.0的写法 28 db.Set<T>().Attach(entity); 29 db.Entry<T>(entity).State = EntityState.Deleted; 30 return db.SaveChanges() > 0; 31 } 32 33 public IQueryable<T> LoadEntities(Func<T, bool> whereLambda) 34 { 35 //EF4.0的写法 36 //return db.CreateObjectSet<T>().Where<T>(whereLambda).AsQueryable(); 37 //EF5.0的写法 38 return db.Set<T>().Where<T>(whereLambda).AsQueryable(); 39 }
执行SQL语句
1 //EF4.0的写法 2 //int ExcuteSql(string strSql, ObjectParameter[] parameters); 3 return EFContextFactory.GetCurrentDbContext().ExecuteFunction(strSql, parameters); 4 //EF5.0的写法 5 int ExcuteSql(string strSql, DbParameter[] parameters); 6 return DEFContextFactory.GetCurrentDbContext().ExecuteSqlCommand(strSql, parameters);
时间: 2024-10-14 01:50:32