public T AddEntity(T entity) { db.AddObject(entityName, entity); //EF4.0的写法 添加实体 //db.CreateObjectSet<T>().AddObject(entity); //EF5.0的写法 //db.Entry<T>(entity).State = EntityState.Added; db.SaveChanges(); return entity; } /// <summary> /// 实现对数据库的修改功能 /// </summary> /// <param name="entity"></param> /// <returns></returns> public bool UpdateEntity(T entity) { //EF4.0的写法 // db.CreateObjectSet<T>().Attach(entity); //db.ObjectStateManager.ChangeObjectState(entity, EntityState.Modified); //EF5.0的写法 //db.Set<T>().Attach(entity); //db.Entry<T>(entity).State = EntityState.Modified; return db.SaveChanges() > 0; } /// <summary> /// 实现对数据库的删除功能 /// </summary> /// <param name="entity"></param> /// <returns></returns> public bool DeleteEntity(T entity) { db.DeleteObject(entity); //EF4.0的写法 //db.CreateObjectSet<T>().Attach(entity); //db.ObjectStateManager.ChangeObjectState(entity, EntityState.Deleted); //EF5.0的写法 //db.Set<T>().Attach(entity); //db.Entry<T>(entity).State = EntityState.Deleted; return db.SaveChanges() > 0; }
如题,实现了add和delete,请问如何实现update?
其实什么语句都不用添加就行了,在应用层修改,DAL层调用SaveChanges()。