Ejemplo n.º 1
0
 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..");
             }
         }
     });
 }
Ejemplo n.º 2
0
        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);
        }
Ejemplo n.º 3
0
        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);
        }
Ejemplo n.º 4
0
        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);
        }