Beispiel #1
0
 private void buttonClearDb(object sender, RoutedEventArgs e)
 {
     DatabaseContext context = new DatabaseContext();
     context.Comments.RemoveRange(context.Comments);
     context.Product.RemoveRange(context.Product);
     context.SaveChanges();
 }
Beispiel #2
0
        /// <summary>
        /// Funcja dodająca proddukt do bd, wraz ze sprawdzaniem czy dany komentarz dla danego produktu istnieje
        /// </summary>
        /// <param name="product"></param>
        /// <param name="statistic"></param>
        private void addProductToDatabase(Product product, IStatisctics statistic)
        {
            try
            {
                using (var db = new DatabaseContext())
                {
                    IQueryable<Product> productsInDb = from p in db.Product
                                                       where
                         p.Brand.Equals(product.Brand) &&
                         p.Model.Equals(product.Model) &&
                         p.Type.Equals(product.Type)
                                                       select p;// select db.Product;//and p.Model.E;

                    if (productsInDb != null)
                    {
                        foreach (Product productInDb in productsInDb)
                        {
                            DateTime time = DateTime.Now;

                            foreach (CommentDb dowloadedProd in product.Comments)
                            {
                                bool contains = productInDb.Comments.Any(x => {
                                    bool returnValue = true;
                                    if (dowloadedProd.Advantages != null && x.Advantages != null)
                                        returnValue &= dowloadedProd.Advantages.Equals(x.Advantages);
                                    if (dowloadedProd.Disadvantages != null && x.Disadvantages != null)
                                        returnValue &= dowloadedProd.Disadvantages.Equals(x.Disadvantages);
                                    if (dowloadedProd.Comment != null && x.Comment != null)
                                        returnValue &= dowloadedProd.Comment.Equals(x.Comment);
                                    if (dowloadedProd.Date != null && x.Date != null)
                                        returnValue &= dowloadedProd.Date.Equals(x.Date);
                                    returnValue &= dowloadedProd.Recommend.Equals(x.Recommend);
                                    returnValue &= dowloadedProd.Stars.Equals(x.Stars);
                                    returnValue &= dowloadedProd.Usability.Equals(x.Usability);
                                    returnValue &= dowloadedProd.UsabilityVotes.Equals(x.UsabilityVotes);
                                    returnValue &= dowloadedProd.Author.Equals(x.Author);
                                    return returnValue;
                                });
                                if (contains) { }
                                else
                                {
                                    dowloadedProd.LoadDate = time;
                                    statistic.addAddedComment(dowloadedProd.Comment);

                                    productInDb.Comments.Add(dowloadedProd);
                                }
                            }
                        }
                        if (productsInDb.Count() == 0)
                        {
                            DateTime time = DateTime.Now;
                            foreach (CommentDb com in product.Comments)
                            {
                                com.LoadDate = time;
                                statistic.addAddedComment(com.Comment);
                            }

                            db.Product.Add(product);
                        }

                    }
                    else
                    {
                        DateTime time = DateTime.Now;
                        foreach (CommentDb com in product.Comments)
                        {
                                com.LoadDate = time;
                                statistic.addAddedComment(com.Comment);
                        }
                        db.Product.Add(product);
                    }
                    db.SaveChanges();
                }
            }
            catch (System.Data.Entity.Validation.DbEntityValidationException ex)
            {
                // Retrieve the error messages as a list of strings.
                var errorMessages = ex.EntityValidationErrors
                        .SelectMany(x => x.ValidationErrors)
                        .Select(x => x.ErrorMessage);

                // Join the list to a single string.
                var fullErrorMessage = string.Join("; ", errorMessages);

                // Combine the original exception message with the new one.
                var exceptionMessage = string.Concat(ex.Message, " The validation errors are: ", fullErrorMessage);

                // Throw a new DbEntityValidationException with the improved exception message.
                throw new System.Data.Entity.Validation.DbEntityValidationException(exceptionMessage, ex.EntityValidationErrors);
            }
        }