1 public T AddEntity(T entity) 2 { 3 //EF4.0的写法 4 添加实体 5 //db.CreateObjectSet().AddObject(entity); 6 //EF5.0的写法 7 db.Entry (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 ().Addach(entity);16 //db.ObjectStateManager.ChangeObjectState(entity, EntityState.Modified);17 //EF5.0的写法 18 db.Set ().Attach(entity); 19 db.Entry (entity).State = EntityState.Modified; 20 return db.SaveChanges() > 0; 21 }22 public bool DeleteEntity(T entity)23 {24 //EF4.0的写法 25 //db.CreateObjectSet ().Addach(entity);26 //db.ObjectStateManager.ChangeObjectState(entity, EntityState.Deleted);27 //EF5.0的写法28 db.Set ().Attach(entity);29 db.Entry (entity).State = EntityState.Deleted;30 return db.SaveChanges() > 0;31 }32 33 public IQueryable LoadEntities(Func whereLambda)34 {35 //EF4.0的写法36 //return db.CreateObjectSet ().Where (whereLambda).AsQueryable();37 //EF5.0的写法38 return db.Set ().Where (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);