public void AddCart(int Id)
 {
     using (var db = new AppContextForBook())
     {
         var book = db.Books.Where(x => x.Id == Id).FirstOrDefault();
     }
 }
 public ActionResult Create(NewBook model)
 {
     try
     {
         using (var db = new AppContextForBook())
         {
             var book = new Book
             {
                 Name        = model.Name,
                 Author      = model.Author,
                 Price       = model.Price,
                 Popular     = model.Popular,
                 PublishDate = model.PublishDate
             };
             try
             {
                 db.Books.AddRange(book);
                 db.SaveChanges();
             }
             catch (Exception)
             {
                 throw;
             }
         }
         return(RedirectToAction(nameof(Details)));
     }
     catch
     {
         return(View());
     }
 }
 // GET: Books/Details/5
 public ActionResult Details(int Id)
 {
     using (var db = new AppContextForBook())
     {
         var book = db.Books.Where(x => x.Id == Id).FirstOrDefault();
         return(View(book));
     }
 }
示例#4
0
 public static void Main(string[] args)
 {
     using (AppContextForBook db = new AppContextForBook())
     {
         db.Database.EnsureCreated();
     }
     CreateWebHostBuilder(args).Build().Run();
 }
        public IActionResult Index()
        {
            List <Book> books = new List <Book>();

            using (var db = new AppContextForBook())
            {
                books = db.Books.Select(book => new Book
                {
                    Id          = book.Id,
                    Name        = book.Name,
                    Author      = book.Author,
                    PublishDate = book.PublishDate,
                    Price       = book.Price,
                    Popular     = book.Popular
                }).ToList();
            };
            return(View(books));
        }
示例#6
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, AppContextForBook context)
        {
            context.Database.Migrate();
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseCookiePolicy();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
        }