예제 #1
0
 public async Task <IActionResult> Create([Bind("FirstName, LastName")] Author author)
 {
     using (EFCoreWebDemoContext context = new EFCoreWebDemoContext())
     {
         //context.Add(author);
         SqlParameter[] parameters = new SqlParameter[] {
             new SqlParameter {
                 ParameterName = "@AuthorId", SqlDbType = SqlDbType.Int, Direction = ParameterDirection.Output
             },
             new SqlParameter {
                 ParameterName = "@FirstName", SqlDbType = SqlDbType.VarChar, Size = 50, Direction = ParameterDirection.Input, SqlValue = author.FirstName
             },
             new SqlParameter {
                 ParameterName = "@LastName", SqlDbType = SqlDbType.VarChar, Size = 75, Direction = ParameterDirection.Input, SqlValue = author.LastName
             }
         };
         try
         {
             context.Database.ExecuteSqlRaw("Exec AuthorSave @AuthorId OUT, @FirstName, @LastName", parameters);
         }
         catch (System.Exception e)
         {
             //log error
             ModelState.TryAddModelError("AddingAuthor", e.Message);
         }
         object returnvalue = parameters[0].Value;
         if (returnvalue != null && int.TryParse(returnvalue.ToString(), out int AuthorId) && AuthorId > 0)
         {
             //Log success message;
         }
         //await context.SaveChangesAsync();
         return(RedirectToAction("Index"));
     }
 }
 public string[] Get()
 {
     using (var context = new EFCoreWebDemoContext())
     {
         return(context.ListName.Select(r => r.Title).ToArray());
     }
 }
예제 #3
0
        // GET: Books/Edit/5
        public async Task <IActionResult> Edit(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }
            using (var _context = new EFCoreWebDemoContext())
            {
                var book = await _context.Books.FindAsync(id);

                if (book == null)
                {
                    return(NotFound());
                }
                var authors = await _context.Authors.Select(a => new SelectListItem
                {
                    Value = a.AuthorId.ToString(),
                    Text  = $"{a.FirstName} {a.LastName}"
                }).ToListAsync();

                ViewBag.Authors = authors;
                //ViewData["AuthorId"] = new SelectList(_context.Authors, "AuthorId", "AuthorId", book.AuthorId);
                return(View(book));
            }
        }
예제 #4
0
        public async Task <IActionResult> Edit(int id, [Bind("AuthorId,FirstName,LastName")] Author author)
        {
            if (id != author.AuthorId)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    using (var context = new EFCoreWebDemoContext())
                    {
                        context.Update(author);
                        await context.SaveChangesAsync();
                    }
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!AuthorExists(author.AuthorId))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            //return View(author);
            return(RedirectToAction("Index"));
        }
예제 #5
0
 private bool BookExists(int id)
 {
     using (var _context = new EFCoreWebDemoContext())
     {
         return(_context.Books.Any(e => e.BookId == id));
     }
 }
예제 #6
0
 private bool AuthorExists(int id)
 {
     using (var context = new EFCoreWebDemoContext())
     {
         return(context.Authors.Any(e => e.AuthorId == id));
     }
 }
 public ActionResult <string> Get(int id)
 {
     using (var context = new EFCoreWebDemoContext())
     {
         return(context.ListName.Where(r => r.ListId == id).Select(r => r.Title).First());
     }
 }
        public async Task <IActionResult> Index()
        {
            using (var context = new EFCoreWebDemoContext())
            {
                var model = await _iBookServices.GetAll();

                return(View(model));
            }
        }
예제 #9
0
        public async Task <IActionResult> Index()
        {
            using (EFCoreWebDemoContext context = new EFCoreWebDemoContext())
            {
                System.Collections.Generic.List <Author> model = await context.Authors.Include(a => a.Books).AsNoTracking().ToListAsync();

                return(View(model));
            }
        }
예제 #10
0
        public async Task <List <Book> > GetAll()
        {
            using (var context = new EFCoreWebDemoContext())
            {
                var model = await context.Books.ToListAsync();

                return(model);
            }
        }
예제 #11
0
        public async Task <IActionResult> Index()
        {
            using (var context = new EFCoreWebDemoContext())
            {
                var model = await context.Authors.Include(a => a.Books).AsNoTracking().ToListAsync();

                return(View(model));
            }
        }
예제 #12
0
        public async Task <Book> GetBookById(int id)
        {
            using (var context = new EFCoreWebDemoContext())
            {
                var model = await context.Books.FirstOrDefaultAsync(x => x.BookId == id);

                return(model);
            }
        }
예제 #13
0
        public async Task <IActionResult> Create([Bind("Title, AuthorId")] Book book)
        {
            using (var context = new EFCoreWebDemoContext())
            {
                context.Books.Add(book);
                await context.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
        }
예제 #14
0
        public async Task <IActionResult> Create([Bind("FirstName, LastName")] Author author)
        {
            using (var context = new EFCoreWebDemoContext())
            {
                context.Add(author);
                await context.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
        }
예제 #15
0
        public async Task <IActionResult> DeleteConfirmed(int id)
        {
            using (var _context = new EFCoreWebDemoContext())
            {
                var book = await _context.Books.FindAsync(id);

                _context.Books.Remove(book);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
        }
예제 #16
0
        public async Task <IActionResult> DeleteConfirmed(int id)
        {
            using (var context = new EFCoreWebDemoContext())
            {
                var author = await context.Authors.FindAsync(id);

                context.Authors.Remove(author);
                await context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
        }
예제 #17
0
        public async Task <IActionResult> Create()
        {
            using (var context = new EFCoreWebDemoContext())
            {
                var authors = await context.Authors.Select(a => new SelectListItem {
                    Value = a.AuthorId.ToString(),
                    Text  = $"{a.FirstName} {a.LastName}"
                }).ToListAsync();

                ViewBag.Authors = authors;
            }
            return(View());
        }
예제 #18
0
        // GET: Author/Edit/5
        public async Task <IActionResult> Edit(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }
            using (var context = new EFCoreWebDemoContext())
            {
                var author = await context.Authors.FindAsync(id);

                if (author == null)
                {
                    return(NotFound());
                }
                return(View(author));
            }
        }
예제 #19
0
        // GET: Author/Delete/5
        public async Task <IActionResult> Delete(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }
            using (var context = new EFCoreWebDemoContext())
            {
                var author = await context.Authors
                             .FirstOrDefaultAsync(m => m.AuthorId == id);

                if (author == null)
                {
                    return(NotFound());
                }
                return(View(author));
            }
        }
예제 #20
0
        // GET: Books/Details/5
        public async Task <IActionResult> Details(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }
            using (var _context = new EFCoreWebDemoContext())
            {
                var book = await _context.Books
                           .Include(b => b.Author)
                           .FirstOrDefaultAsync(m => m.BookId == id);

                if (book == null)
                {
                    return(NotFound());
                }
                return(View(book));
            }
        }
예제 #21
0
 protected override void Seed()
 {
     using (var context = new EFCoreWebDemoContext())
     {
         context.Add(new AuctionItems
         {
             auctionItemId       = 1234,
             BidderName          = "XYZ industries",
             CurrentBid          = 0,
             reservePrice        = 100,
             maxAutoBidAmountmax = 10
         });
         context.Add(new AuctionItems
         {
             auctionItemId       = 1233,
             BidderName          = "AMC industries",
             CurrentBid          = 10,
             reservePrice        = 1200,
             maxAutoBidAmountmax = 1120
         });
         context.Add(new AuctionItems
         {
             auctionItemId       = 443,
             BidderName          = "AMC industries",
             CurrentBid          = 1001,
             reservePrice        = 12200,
             maxAutoBidAmountmax = 11320
         });
         context.Add(new AuctionItems
         {
             auctionItemId       = 555,
             BidderName          = "AMC industries",
             CurrentBid          = 10,
             reservePrice        = 12010,
             maxAutoBidAmountmax = 11220
         });
     }
 }
예제 #22
0
        public async Task <IActionResult> Create([Bind("Title, AuthorId")] Book book)
        {
            if (ModelState.IsValid)
            {
                using (var _context = new EFCoreWebDemoContext())
                {
                    _context.Add(book);
                    await _context.SaveChangesAsync();

                    return(RedirectToAction(nameof(Index)));
                }
            }
            using (var _context = new EFCoreWebDemoContext())
            {
                ViewData["AuthorId"] = new SelectList(_context.Authors, "AuthorId", "AuthorId", book.AuthorId);
            }
            return(View(book));
            //using (var context = new EFCoreWebDemoContext())
            //{
            //    context.Books.Add(book);
            //    await context.SaveChangesAsync();
            //    return RedirectToAction("Index");
            //}
        }
예제 #23
0
        public async Task <IActionResult> Edit(int id, [Bind("BookId,Title,AuthorId")] Book book)
        {
            if (id != book.BookId)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    using (var _context = new EFCoreWebDemoContext())
                    {
                        _context.Update(book);
                        await _context.SaveChangesAsync();
                    }
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!BookExists(book.BookId))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            using (var _context = new EFCoreWebDemoContext())
            {
                ViewData["AuthorId"] = new SelectList(_context.Authors, "AuthorId", "AuthorId", book.AuthorId);
            }
            return(View(book));
        }
예제 #24
0
 public StarWarsEFService(EFCoreWebDemoContext context)
 {
     _context = context;
 }