Exemplo n.º 1
0
        public void Run()
        {
            var testLimit = 1000000;

            var pubs = GetPublishers().Take(testLimit);

            _c.AddRange(pubs);
            _c.SaveChanges();

            var authors = GetAuthors().Take(testLimit);

            _c.AddRange(authors);
            _c.SaveChanges();


            IEnumerable <Subject> subjects = GetSubjects().Take(testLimit);

            _c.AddRange(subjects);
            _c.SaveChanges();

            SeedCategories();

            pubs     = _c.Query <Publisher>().ToList();
            authors  = _c.Query <Author>().ToList();
            subjects = _c.Query <Subject>().ToList();
            var cats = _c.Query <Category>().ToList();

            IEnumerable <Book> books = GetBooks(pubs, authors, cats, subjects).Take(testLimit);

            foreach (var b in books)
            {
                try
                {
                    _c.Add(b);
                    _c.SaveChanges();
                }
                catch (DbEntityValidationException e)
                {
                    _log.Error("Could not save book {0}, reason: {1}", b.Title, e.Message);
                    foreach (var eve in e.EntityValidationErrors)
                    {
                        _log.Error("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                                   eve.Entry.Entity.GetType().Name, eve.Entry.State);
                        foreach (var ve in eve.ValidationErrors)
                        {
                            _log.Error("- Property: \"{0}\", Error: \"{1}\"",
                                       ve.PropertyName, ve.ErrorMessage);
                        }
                    }
                    _c.LocalClear <Book>();
                }
                catch (Exception ex)
                {
                    _log.Error("Could not save book {0}, reason: {1}", b.Title, ex.Message);
                }
            }
        }
Exemplo n.º 2
0
        public ActionResult Create([Bind(Include = "BookID,Title,AlternativeTitle,AuthorID,CategoryID,PublisherID,InitialPrintedYear,ActualPrintYear,Language,Material,Read,Pages,ISBN,Website,CoverLink,Rating,CodeWithinSerie,Condition,ReviewNote,Location")] Book book)
        {
            if (ModelState.IsValid)
            {
                _db.Add(book);
                _db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            PopulateCategoryDropDownList(book.CategoryID);
            PopulatePublishersDropDownList(book.PublisherID);


            return(View(book));
        }
Exemplo n.º 3
0
        public Book AddBook(Book newBook)
        {
            if (null == newBook)
            {
                throw new ArgumentNullException("newBook");
            }
            if (string.IsNullOrEmpty(newBook.Title))
            {
                throw new ArgumentException("Title is a required field.", "newBook");
            }
            if (string.IsNullOrEmpty(newBook.ISBN))
            {
                throw new ArgumentException("ISBN is a required field.", "newBook");
            }
            if (null == newBook.Authors || 0 >= newBook.Authors.Count)
            {
                throw new ArgumentException("At least one author should be provided", "newBook");
            }


            _bookContext.Books.Add(newBook);
            _bookContext.SaveChanges();

            return(newBook);
        }