public static int SaveNewUser(user MyUser) { //get method name string MethodName = System.Reflection.MethodBase.GetCurrentMethod().Name; //initialize entities var ctx = new rentalanguageteacherEntities(); { try { log.Debug(MethodName + "**********************************************************************************************************"); log.Debug(MethodName + "() - Start"); //Check object state, add or update if (MyUser.user_id == 0) { //Add new object ctx.users.Add(MyUser); log.Debug(MethodName + "() - New object added"); } else { //Update object // ctx.users.Attach(MyUser); //ctx.ObjectStateManager.ChangeObjectState(stud, System.Data.EntityState.Modified); ctx.Entry(MyUser).State = System.Data.EntityState.Detached; ctx.Entry(MyUser).State = System.Data.EntityState.Modified; // log.Debug(MethodName + "() - The object for ID " + MyUser.user_id + " was updated"); } //save context changes ctx.SaveChanges(); //return? return(MyUser.user_id); } catch (Exception e) { log.Error(MethodName + "() - " + e.Message); log.Debug(MethodName + "() - " + e.StackTrace); throw (e); } finally { log.Debug(MethodName + "() - Finish"); log.Debug(MethodName + "**********************************************************************************************************"); } } }
public static int DeleteUser(user MyUser) { //get method name string MethodName = System.Reflection.MethodBase.GetCurrentMethod().Name; //initialize entities var ctx = new rentalanguageteacherEntities(); { try { log.Debug(MethodName + "**********************************************************************************************************"); log.Debug(MethodName + "() - Start"); ctx.Entry(MyUser).State = System.Data.EntityState.Deleted; //Delete user ctx.users.Remove(MyUser); //save context changes int num = ctx.SaveChanges(); //return? return(num); } catch (Exception e) { log.Error(MethodName + "() - " + e.Message); log.Debug(MethodName + "() - " + e.StackTrace); throw (e); } finally { log.Debug(MethodName + "() - Finish"); log.Debug(MethodName + "**********************************************************************************************************"); } } }