コード例 #1
0
        public static void SeedDatabase(this BookContext dbContext)
        {
            dbContext.Books.Add
            (
                new Book
            {
                Id        = 1,
                Title     = "Book_01",
                Isbn      = "1234567890123",
                Published = Convert.ToDateTime("2000/10/25")
            }
            );

            dbContext.Add
            (
                new Book
            {
                Id        = 2,
                Title     = "Book_02",
                Isbn      = "2345678901231",
                Published = Convert.ToDateTime("2009/10/25")
            }
            );

            dbContext.Add
            (
                new Book
            {
                Id        = 3,
                Title     = "Book_03",
                Isbn      = "3456789012312",
                Published = Convert.ToDateTime("2019/01/11")
            }
            );

            dbContext.Add
            (
                new Book
            {
                Id        = 4,
                Title     = "Book_04",
                Isbn      = "4567890123123",
                Published = Convert.ToDateTime("1999/02/09")
            }
            );

            dbContext.Add
            (
                new Book
            {
                Id        = 5,
                Title     = "Book_05",
                Isbn      = "5678901231234",
                Published = Convert.ToDateTime("2015/06/28")
            }
            );

            dbContext.SaveChanges();
        }
コード例 #2
0
        public IActionResult Order(int id)
        {
            var CurentUserId = User.FindFirstValue(ClaimTypes.NameIdentifier);
            var Order        = _context.Orders.SingleOrDefault(o => o.UserId == CurentUserId && !o.IsFainaly);

            if (Order == null)
            {
                var details = new Order
                {
                    CreateDate = DateTime.Now,
                    Sum        = 0,
                    IsFainaly  = false,
                    UserId     = CurentUserId,
                };
                _context.Add(details);
                _context.SaveChanges();
                _context.Add(new OrdeeDetails
                {
                    OrderId = details.OrderId,
                    Price   = _context.BookStors.Find(id).Price,
                    Count   = 1,
                    BookId  = id
                });
                _context.SaveChanges();
                SumOrder(details.OrderId);
            }
            else
            {
                var Detail = _context.OrdeeDetails.SingleOrDefault(d => d.OrderId == Order.OrderId && d.BookId == id);
                if (Detail == null)
                {
                    _context.Add(new OrdeeDetails
                    {
                        OrderId = Order.OrderId,
                        Price   = _context.BookStors.Find(id).Price,
                        Count   = 1,
                        BookId  = id
                    });
                }
                else
                {
                    Detail.Count += 1;
                    _context.Update(Detail);
                    SumOrder(Detail.OrderId);
                }
                _context.SaveChanges();
            }


            return(RedirectToAction("ShowOrder"));
        }
コード例 #3
0
        public BookController(BookContext context)
        {
            _context = context;

            // Reads the data from the file.
            if (_context.BookItem.Count() != 0)
            {
                return;
            }
            XmlDocument bookDoc = new XmlDocument();

            bookDoc.Load("books.xml");

            foreach (XmlNode node in bookDoc.DocumentElement.ChildNodes)
            {
                var nodeList = node.ChildNodes;


                _context.Add(new BookItem
                {
                    Id          = node.Attributes["id"].InnerText,
                    Author      = nodeList[0].InnerText,
                    Title       = nodeList[1].InnerText,
                    Genre       = nodeList[2].InnerText,
                    Price       = double.Parse(nodeList[3].InnerText),
                    PublishDate = Convert.ToDateTime(nodeList[4].InnerText),
                    Description = nodeList[5].InnerText
                });
            }

            _context.SaveChanges();
        }
コード例 #4
0
        public void TestExample()
        {
            //SETUP
            var options = this
                          .CreateUniqueClassOptions <BookContext>();

            using (var context = new BookContext(options))
            {
                context.CreateEmptyViaWipe();
                var logs = context.SetupLogging();

                //ATTEMPT
                context.Add(new Book {
                    Title = "New Book"
                });
                context.SaveChanges();

                //VERIFY
                context.Books.Count().ShouldEqual(1);
                foreach (var log in logs.ToList())
                {
                    _output.WriteLine(log.ToString());
                }
            }
        }
コード例 #5
0
        public void TestEfCoreLoggingStringWithBadValues()
        {
            //SETUP
            var logs = new List <LogOutput>();

#pragma warning disable 618
            var options = SqliteInMemory.CreateOptionsWithLogging <BookContext>(log => logs.Add(log));
#pragma warning restore 618
            using (var context = new BookContext(options))
            {
                context.Database.EnsureCreated();

                //ATTEMPT
                context.Books.Count();
                context.Add(new Book {
                    Title = "The person's boss said, \"What's that about?\""
                });
                context.SaveChanges();

                //VERIFY
                foreach (var log in logs.ToList())
                {
                    _output.WriteLine(log.ToString());
                }
            }
        }
コード例 #6
0
        public void TestResetKeysSingleEntityWithRelationshipsCheckStateIsAdded()
        {
            //SETUP
            var  options = SqliteInMemory.CreateOptions <BookContext>();
            Book entity;

            using (var context = new BookContext(options))
            {
                context.Database.EnsureCreated();
                context.SeedDatabaseFourBooks();
                entity = context.Books
                         .Include(x => x.Reviews)
                         .Include(x => x.Promotion)
                         .Include(x => x.AuthorsLink)
                         .ThenInclude(x => x.Author)
                         .First(x => x.Reviews.Any());

                var resetter = new DataResetter(context);
                resetter.ResetKeysEntityAndRelationships(entity);
            }
            using (var context = new BookContext(options))
            {
                //ATTEMPT
                context.Add(entity);
                var states = context.ChangeTracker.Entries().Select(x => x.State).ToList();

                //VERIFY
                states.Count.ShouldEqual(1 + 2 + 1 + 1 + 1); // Book + 2 reviews + promotion + BookAuthor + author
                states.All(x => x == EntityState.Added).ShouldBeTrue();
            }
        }
コード例 #7
0
        public Name insertName(Name name)
        {
            context.Add(name);
            context.SaveChanges();

            return(name);
        }
コード例 #8
0
        private static void AddBook()
        {
            var book = new Book()
            {
                Title    = "Enterprise Applicatio Patterns Using Xamarin.Forms",
                Chapters = new List <Chapter>
                {
                    new Chapter()
                    {
                        Number = 1, Title = "Introduction"
                    },
                    new Chapter()
                    {
                        Number = 2, Title = "MVVM"
                    },
                    new Chapter()
                    {
                        Number = 3, Title = "Dependency Injection"
                    },
                    new Chapter()
                    {
                        Number = 4, Title = "Navigation"
                    }
                }
            };

            using (var context = new BookContext())
            {
                context.GetService <ILoggerFactory>().AddProvider(new MyLoggerProvider());
                context.Add(book);
                context.SaveChanges();
            }
        }
コード例 #9
0
        public void TestWhatHappensIfDoNotResetPkFk()
        {
            //SETUP
            var  options = SqliteInMemory.CreateOptions <BookContext>();
            Book entity;

            using (var context = new BookContext(options))
            {
                context.Database.EnsureCreated();
                context.SeedDatabaseFourBooks();
                entity = context.Books
                         .Include(x => x.AuthorsLink)
                         .ThenInclude(x => x.Author)
                         .First();
            }
            using (var context = new BookContext(options))
            {
                //ATTEMPT
                context.Add(entity);
                var ex = Assert.Throws <DbUpdateException>(() => context.SaveChanges());

                //VERIFY
                ex.InnerException.Message.ShouldEqual("SQLite Error 19: 'UNIQUE constraint failed: Authors.AuthorId'.");
            }
        }
コード例 #10
0
 public Author Add(Author author)
 {
     // TODO: implement add
     _bookContext.Add(author);
     _bookContext.SaveChanges();
     return(author);
 }
コード例 #11
0
        public Book Post(Book newBook)
        {
            _bookContext.Add(newBook);
            _bookContext.SaveChanges();

            return(newBook);
        }
コード例 #12
0
        public void TestExample()
        {
            //SETUP
            var logs    = new List <string>();
            var options = this.CreateUniqueClassOptionsWithLogTo <BookContext>(log => logs.Add(log));

            using (var context = new BookContext(options))
            {
                context.Database.EnsureClean();
                logs.Clear();

                //ATTEMPT
                context.Add(new Book {
                    Title = "New Book"
                });
                context.SaveChanges();

                //VERIFY
                context.Books.Count().ShouldEqual(1);
                foreach (var log in logs)
                {
                    _output.WriteLine(log);
                }
            }
        }
コード例 #13
0
 public void AddCategory(Category newCategory)
 {
     if (GetCategoryByName(newCategory.Name) == null)
     {
         _context.Add(newCategory);
         _context.SaveChanges();
     }
 }
コード例 #14
0
        public void AddBook(Book book)
        {
            if (book == null)
            {
                throw new ArgumentNullException(nameof(book));
            }

            _context.Add(book);
        }
コード例 #15
0
        public void AddBook(Book bookToAdd)
        {
            if (bookToAdd == null)
            {
                throw new ArgumentNullException(nameof(bookToAdd));
            }

            _bookContext.Add(bookToAdd);
        }
コード例 #16
0
        public Book Post(Book newBook)
        {
            newBook.Id = _nextId++;

            _bookContext.Add(newBook);

            _bookContext.SaveChanges();

            return(newBook);
        }
コード例 #17
0
ファイル: BooksController.cs プロジェクト: 845263/Tasks
        public async Task <IActionResult> Create([Bind("Bid,Btitle,Category,Price,Authorname,Publisher,ReleaseDate")] Book book)
        {
            if (ModelState.IsValid)
            {
                _context.Add(book);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(book));
        }
コード例 #18
0
ファイル: FeedbacksController.cs プロジェクト: Keparisss/lab7
        public async Task <IActionResult> Create([Bind("Id,BookId,AboutBook")] Feedback feedback)
        {
            if (ModelState.IsValid)
            {
                _context.Add(feedback);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(feedback));
        }
コード例 #19
0
ファイル: BooksController.cs プロジェクト: st-1297/DotNetCore
        public async Task <IActionResult> Create([Bind("Id,Title,Price,Publisher,Sample,PublishYear")] Book book)
        {
            if (ModelState.IsValid)
            {
                _context.Add(book);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(book));
        }
コード例 #20
0
        public async Task <IActionResult> Create(BookModel bookModel)
        {
            if (ModelState.IsValid)
            {
                _context.Add(bookModel);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(bookModel));
        }
コード例 #21
0
        public async Task <IActionResult> Create([Bind("Id,name")] Specialist specialist)
        {
            if (ModelState.IsValid)
            {
                _context.Add(specialist);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(specialist));
        }
コード例 #22
0
        public async Task <IActionResult> Create([Bind("ID,Title,Author,Price,SKU")] Book book)
        {
            if (ModelState.IsValid)
            {
                _context.Add(book);
                await _context.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
            return(View(book));
        }
コード例 #23
0
        public async Task <IActionResult> Create([Bind("Id,Name")] Verlag verlag)
        {
            if (ModelState.IsValid)
            {
                _context.Add(verlag);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(verlag));
        }
コード例 #24
0
        public async Task <IActionResult> Create([Bind("Id,Name,Brand,Model")] Printer printer)
        {
            if (ModelState.IsValid)
            {
                _context.Add(printer);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(printer));
        }
コード例 #25
0
ファイル: BooksController.cs プロジェクト: Keparisss/lab7
        public async Task <IActionResult> Create([Bind("ID,Name,Author,Genre,Price")] Book book)
        {
            if (ModelState.IsValid)
            {
                _context.Add(book);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(book));
        }
コード例 #26
0
        public async Task <IActionResult> Create([Bind("BookFormatID,FormatDescription")] BookFormat bookFormat)
        {
            if (ModelState.IsValid)
            {
                _context.Add(bookFormat);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(bookFormat));
        }
コード例 #27
0
        public async Task <IActionResult> Create([Bind("Id,Title,Slug,Author,Publisher,PublishedDate,Description,Isbn10,Isbn13,PageCount,Thumbnail,InfoLink,Language,Category")] Book book)
        {
            if (ModelState.IsValid)
            {
                _context.Add(book);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(book));
        }
コード例 #28
0
        public async Task <IActionResult> Create([Bind("AuthorId,Name")] Author author)
        {
            if (ModelState.IsValid)
            {
                dataContext.Add(author);
                await dataContext.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(author));
        }
コード例 #29
0
        public async Task <IActionResult> Create([Bind("CategoryID,CategoryDescription")] Category category)
        {
            if (ModelState.IsValid)
            {
                _context.Add(category);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(category));
        }
        public async Task <IActionResult> Create([Bind("IdBook,Zaglavie,Avtor,Janr,Kolichestvo")] Book book)
        {
            if (ModelState.IsValid)
            {
                _context.Add(book);
                await _context.SaveChangesAsync();

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