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); }
public static async Task <List <Book> > GetAllData() { List <Book> result = new List <Book>(); using (BooksEntities db = new BooksEntities()) { try { result = await db.Books.ToListAsync(); } catch (Exception ex) { Console.WriteLine(ex.Message); } } Console.WriteLine(Thread.CurrentThread.ManagedThreadId); return(result); }
public static async Task <Book> GetById(int?id) { if (!id.HasValue) { return(null); } Book book = new Book(); using (BooksEntities db = new BooksEntities()) { try { book = await db.Books.FindAsync(id); } catch (Exception ex) { Console.WriteLine(ex.Message); } } return(book); }