public static void RemoveBooks(string bookId)//删除书籍(对单本书进行操作) { using (var db = new BookShelfContext()) { var oldBooks = db.Books.Where(book => book.BookId == bookId); db.Books.RemoveRange(oldBooks); db.SaveChanges(); } }
private static void RemoveBooksFromShelf(string bookShelfId)//删除书籍(对整个书架进行操作),可能无用 { using (var db = new BookShelfContext()) { var oldBooks = db.Books.Where(book => book.BookShelfId == bookShelfId); db.Books.RemoveRange(oldBooks); db.SaveChanges(); } }
public static void UpdateBook(Book newBook)//更新数据库内的书籍 { RemoveBooks(newBook.BookId); using (var db = new BookShelfContext()) { db.Entry(newBook).State = EntityState.Modified; db.Books.Add(newBook); db.SaveChanges(); } }
public static void UpdateShelf(BookShelf newBookShelf)//更新数据库内的书架 { RemoveBooksFromShelf(newBookShelf.BookShelfId); using (var db = new BookShelfContext()) { db.Entry(newBookShelf).State = EntityState.Modified; db.Books.AddRange(newBookShelf.Books); db.SaveChanges(); } }
public static void AddAdministrator(Client admin)//添加管理员 { try { using (var db = new BookShelfContext()) { db.Clients.Add(admin); db.SaveChanges(); } } catch (Exception e) { throw new ApplicationException($"添加管理员错误: {e.Message}"); } }
private static void ClearBooks() { try { using (var db = new BookShelfContext()) { var oldBooks = db.Books.Where(book => book.BookShelfId == null); db.Books.RemoveRange(oldBooks); db.SaveChanges(); } } catch (Exception e) { MessageBox.Show("删除书架错误!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information); throw new ApplicationException($"删除书架错误!"); } }
public static void RemoveBookShelf(string id) { try { using (var db = new BookShelfContext()) { var shelf = db.BookShelves.Include("Books").Where(o => o.BookShelfId == id).FirstOrDefault(); db.BookShelves.Remove(shelf); db.SaveChanges(); ClearBooks(); } } catch (Exception e) { MessageBox.Show("删除书架错误!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information); throw new ApplicationException($"删除书架错误!"); } }
public static BookShelf AddBookShelf(BookShelf shelf)//添加新的书架 { try { using (var db = new BookShelfContext()) { db.BookShelves.Add(shelf); db.SaveChanges(); MessageBox.Show("已成功添加书架!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } return(shelf); } catch (Exception e) { MessageBox.Show("添加书架错误!" + e.ToString(), "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information); throw new ApplicationException($"添加书架错误: {e.Message}"); } }
public static bool AddClient(Client client)//添加新的用户 { try { using (var db = new BookShelfContext()) { db.Clients.Add(client); db.SaveChanges(); MessageBox.Show("已成功注册账号!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } return(true); } catch (Exception e) { MessageBox.Show("注册账号错误!" + e.ToString(), "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information); throw new ApplicationException($"添加书架错误: {e.Message}"); } }