public static async Task DeleteById(int?id) { if (!id.HasValue) { return; } await Task.Run(() => { using (BooksEntities db = new BooksEntities()) { bool flag = false; try { Book book = db.Books.FirstOrDefault(p => p.BookId == id); if (book != null) { db.Books.Remove(book); int count = db.SaveChangesAsync().Result; Console.WriteLine($"affecred count {count}"); flag = true; } flag = false; } catch (Exception ex) { flag = false; Console.WriteLine(ex.Message); } finally { Console.WriteLine(flag ? "Succeded.." : "Failed.."); } } }); }
public static async Task <int> UpdateData(Book data, int?id = null) { int affected = 0; using (BooksEntities db = new BooksEntities()) { if (id.HasValue) { try { Book book = db.Books.FirstOrDefault(p => p.BookId == id); if (book != null) { data.BookId = book.BookId; db.Entry(book).CurrentValues.SetValues(data); affected = await db.SaveChangesAsync(); } return(affected); } catch (Exception ex) { Console.WriteLine(ex.Message); } } } return(affected); }