/// <summary> /// 新增或保存诗歌 /// </summary> /// <param name="poem"></param> /// <returns>success or fail</returns> public string SavePoem(Poem poem) { try { using (PoemDbContext db = new PoemDbContext()) { if (poem.PoemID == Guid.Parse("00000000-0000-0000-0000-000000000000"))//无id传进来,是新增 { poem.PoemID = Guid.NewGuid(); db.Poem.Add(poem); } else//保存 { var p = db.Poem.Find(poem.PoemID); p.Title = poem.Title; p.Content = poem.Content; } int res = db.SaveChanges(); return(res > 0 ? "success" : "fail"); } } catch (Exception e) { throw e; } }
/// <summary> /// 新增用户 /// </summary> /// <param name="user">用户实体</param> /// <returns>用户实体</returns> public User AddUser(User user) { try { using (PoemDbContext db = new PoemDbContext()) { user.UserID = Guid.NewGuid(); db.User.Add(user); int res = db.SaveChanges(); return(res > 0 ? user : null); } } catch (Exception e) { throw e; } }
/// <summary> /// 跟新User /// </summary> /// <param name="user"></param> /// <returns>success or fail</returns> public string UpdateUser(User user) { try { int res = 0; using (PoemDbContext db = new PoemDbContext()) { User u = db.User.Find(user.UserID); if (u != null) { u.UserName = user.UserName; u.Password = user.Password; res = db.SaveChanges(); } return(res > 0 ? "success" : "fail"); } } catch (Exception e) { throw e; } }
public void SaveChanges() { _context.SaveChanges(); }