public int AddOrEditClasses(ClassesModel c, UserTokenModel curUser) { if (c.Id > 0) { goto editClass; } var ue = c.Adapt <Classes>(); ue.AddUserId = curUser.UserId; ue.EditUserId = curUser.UserId; ue.AddTime = DateTime.Now; ue.EditTime = DateTime.Now; db.Set <Classes>().Add(ue); return(db.SaveChanges()); editClass: var exist = db.Set <Classes>().Find(c.Id); exist.ClassName = c.ClassName; exist.ClassType = c.ClassType; exist.EditTime = DateTime.Now; exist.EditUserId = curUser.UserId; exist.IsValid = c.IsValid; db.Entry(exist).State = System.Data.Entity.EntityState.Modified; return(db.SaveChanges()); }