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(); }
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")); }
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(); }
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()); } } }
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()); } } }
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(); } }
public Name insertName(Name name) { context.Add(name); context.SaveChanges(); return(name); }
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(); } }
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'."); } }
public Author Add(Author author) { // TODO: implement add _bookContext.Add(author); _bookContext.SaveChanges(); return(author); }
public Book Post(Book newBook) { _bookContext.Add(newBook); _bookContext.SaveChanges(); return(newBook); }
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); } } }
public void AddCategory(Category newCategory) { if (GetCategoryByName(newCategory.Name) == null) { _context.Add(newCategory); _context.SaveChanges(); } }
public void AddBook(Book book) { if (book == null) { throw new ArgumentNullException(nameof(book)); } _context.Add(book); }
public void AddBook(Book bookToAdd) { if (bookToAdd == null) { throw new ArgumentNullException(nameof(bookToAdd)); } _bookContext.Add(bookToAdd); }
public Book Post(Book newBook) { newBook.Id = _nextId++; _bookContext.Add(newBook); _bookContext.SaveChanges(); return(newBook); }
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)); }
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)); }
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)); }
public async Task <IActionResult> Create(BookModel bookModel) { if (ModelState.IsValid) { _context.Add(bookModel); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(bookModel)); }
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)); }
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)); }
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)); }
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)); }
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)); }
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)); }
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)); }
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)); }
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)); }