コード例 #1
0
        public static void Seed(this BookstoreDbContext context)
        {
            if (!context.Role.Any())
            {
                context.Add(new Role {
                    Name = "Admin"
                });
                context.Add(new Role {
                    Name = "User"
                });
                context.SaveChanges();
            }

            if (!context.User.Any())
            {
                var adminUser = new User
                {
                    Email     = "*****@*****.**",
                    FirstName = "Admin",
                    LastName  = "Admin",
                    Username  = "******",
                    Password  = "******"
                };

                context.Add(adminUser);

                context.SaveChanges();

                if (!context.UserRoleMapping.Any())
                {
                    context.UserRoleMapping.Add(new UserRoleMapping {
                        UserId = adminUser.Id, RoleId = context.Role.FirstOrDefault(x => x.Name == "Admin")?.Id ?? throw new InvalidOperationException()
                    });
コード例 #2
0
        public async Task <IActionResult> Create(BookViewModel model)
        {
            if (ModelState.IsValid)
            {
                if (model.BookImage != null && model.BookImage.Length > 0)
                {
                    var fullPath = Path.Combine(_webHostEnvironment.WebRootPath, "uploads", model.BookImage.FileName);
                    using var stream = new FileStream(fullPath, FileMode.Create);
                    model.BookImage.CopyTo(stream);
                }

                var book = new Book
                {
                    Title       = model.Title,
                    Discription = model.Discription,
                    AuthorId    = model.AuthorId,
                    ImagePath   = model.BookImage.FileName
                };

                _context.Add(book);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }

            return(RedirectToAction(nameof(Create)));
        }
コード例 #3
0
        public async Task <IActionResult> Create([Bind("Name,Id,AddedDate")] Author author)
        {
            if (ModelState.IsValid)
            {
                _context.Add(author);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(author));
        }
コード例 #4
0
        public async Task <IActionResult> Create(Book book)
        {
            if (ModelState.IsValid)
            {
                book.ImagePath = UploadImage(book.Image);
                _context.Add(book);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }

            return(View(book));
        }
コード例 #5
0
ファイル: EfRepository.cs プロジェクト: vlajas/bookstore
        public bool Insert(TEntity entity)
        {
            try
            {
                _context.Add(entity);

                _context.SaveChanges();

                return(true);
            }
            catch (Exception e)
            {
                return(false);
            }
        }