title: Validation failed for one or more entities while trying to add a new product
body: <p>When I try to add a new product and therefore add new images to product's folder, this error shows up: </p>
  
  <blockquote>
    <p>Validation failed for one or more entities. Exception Details:
    System.Data.Entity.Validation.DbEntityValidationException: Validation
    failed for one or more entities. See 'EntityValidationErrors' property
    for more details.</p>
  </blockquote>
  
  <pre class="lang-cs prettyprint-override"><code>[HttpGet]
  public ActionResult AddProduct()
  {
      if (Session["User"] == null)
          return RedirectToAction("Login", "Register");
  
      string username = Session["User"].ToString();
      string rolename = Session["Role"].ToString();
      if (rolename == "Seller" || rolename == "Admin")
      {
          var quser = db.Tbl_User.Where(a =&gt; a.User_Username == username &amp;&amp; a.Tbl_Role.Role_Name == rolename).SingleOrDefault();
          if (quser != null)
          {
              var quser1 = db.Tbl_Identity.Where(a =&gt; a.Identity_Userid == quser.User_ID &amp;&amp; a.Identity_Confirm == true).SingleOrDefault();
              if (quser1 != null)
              {
                  return View();
              }
              else
              {
                  TempData["State"] = "Error";
                  return RedirectToAction("Profile", "Profile");
              }
          }
          else
          {
              TempData["State"] = "Error";
              return RedirectToAction("Profile", "Profile");
          }
      }
      else
      {
          TempData["State"] = "Error";
          return RedirectToAction("Profile", "Profile");
      }
  }
  
  [HttpPost]
  public ActionResult AddProduct(Tbl_Product p, HttpPostedFileBase imageindex, HttpPostedFileBase[] picgallery, string TagProduct, string cat1_id)
  {
      if (Session["User"] == null)
          return RedirectToAction("Login", "Register");
  
      string username = Session["User"].ToString();
      string rolename = Session["Role"].ToString();
  
      if (rolename == "Seller" || rolename == "Admin")
      {
          var quser = db.Tbl_User.Where(a =&gt; a.User_Username == username &amp;&amp; a.Tbl_Role.Role_Name == rolename).SingleOrDefault();
          if (quser != null)
          {
              var quser1 = db.Tbl_Identity.Where(a =&gt; a.Identity_Userid == quser.User_ID &amp;&amp; a.Identity_Confirm == true).SingleOrDefault();
              if (quser1 != null)
              {
                  Random rnd = new Random();
  
                  if (imageindex == null)
                  {
                      ViewBag.Error = "chose image";
                      return View();
                  }
  
                  //-------------------------------------------
                  //  Check the image mime types
                  //-------------------------------------------
                  if (imageindex.ContentType.ToLower() != "image/jpg"
                      &amp;&amp; imageindex.ContentType.ToLower() != "image/jpeg"
                      &amp;&amp; imageindex.ContentType.ToLower() != "image/pjpeg"
                      &amp;&amp; imageindex.ContentType.ToLower() != "image/gif"
                      &amp;&amp; imageindex.ContentType.ToLower() != "image/x-png"
                      &amp;&amp; imageindex.ContentType.ToLower() != "image/png")
                  {
                      ViewBag.Error = " bad format ";
                      return View();
                  }
  
                  //-------------------------------------------
                  //  Check the image extension
                  //-------------------------------------------
                  if (Path.GetExtension(imageindex.FileName).ToLower() != ".jpg"
                      &amp;&amp; Path.GetExtension(imageindex.FileName).ToLower() != ".png"
                      &amp;&amp; Path.GetExtension(imageindex.FileName).ToLower() != ".gif"
                      &amp;&amp; Path.GetExtension(imageindex.FileName).ToLower() != ".jpeg")
                  {
                      ViewBag.Error = "  bad file ";
                      return View();
                  }
  
                  if (imageindex.ContentLength &gt;= 512000)
                  {
                      ViewBag.Error = " size error";
                      return View();
                  }
  
                  string rndname = rnd.Next(1, 999).ToString() + "-" + imageindex.FileName;
                  imageindex.SaveAs(Path.Combine(Server.MapPath("~") + "/Content/_images/product/" + rndname));
                  Tbl_Product t = new Tbl_Product();
                  t.Product_PicIndex = rndname;
                  t.Product_AllOff = p.Product_AllOff;
                  t.Product_Date = DateTime.Now;
                  t.Product_ExitCount = p.Product_ExitCount;
                  t.Product_Groupid = Convert.ToInt32(cat1_id);
                  t.Product_IsDownload = p.Product_IsDownload;
                  t.Product_Name = p.Product_Name;
                  t.Product_Off = p.Product_Off;
                  t.Product_Price = p.Product_Price;
                  t.Product_Text = p.Product_Text;
                  t.Product_Userid = quser.User_ID;
                  t.Product_Visit = 0;
                  t.Product_Weight = p.Product_Weight;
                  t.Product_Active = false;
  
                  db.Tbl_Product.Add(t);
  
                  if (Convert.ToBoolean(db.SaveChanges() &gt; 0))
                  {
                      var tagname = TagProduct.Trim().Split('-');
  
                      foreach (var item1 in tagname)
                      {
                          Tbl_Tags tg = new Tbl_Tags()
                          {
                              Tag_Proid = db.Tbl_Product.OrderByDescending(a =&gt; a.Product_ID).FirstOrDefault().Product_ID,
                              Tag_Name = item1.Trim()
                          };
  
                          db.Tbl_Tags.Add(tg);
                          db.SaveChanges();
                      }
  
                      if (picgallery != null)
                      {
                          List&lt;Tbl_Pic&gt; lstpic = new List&lt;Tbl_Pic&gt;();
                          foreach (var item in picgallery)
                          {
                              string namepic = rnd.Next().ToString() + "-" + item.FileName;
  
                              if (item == null)
                              {
                                  var qproid = db.Tbl_Product.OrderByDescending(a =&gt; a.Product_ID).FirstOrDefault();
                                  var qtagid = db.Tbl_Tags.Where(a =&gt; a.Tag_Proid == qproid.Product_ID).ToList();
  
                                  db.Tbl_Tags.RemoveRange(qtagid);
                                  db.Tbl_Product.Remove(qproid);
                                  db.SaveChanges();
  
                                  ViewBag.Error = "chose an image";
                                  return View();
                              }
  
                              //-------------------------------------------
                              //  Check the image mime types
                              //-------------------------------------------
                              if (imageindex.ContentType.ToLower() != "image/jpg"
                                  &amp;&amp; imageindex.ContentType.ToLower() != "image/jpeg"
                                  &amp;&amp; imageindex.ContentType.ToLower() != "image/pjpeg"
                                  &amp;&amp; imageindex.ContentType.ToLower() != "image/gif"
                                  &amp;&amp; imageindex.ContentType.ToLower() != "image/x-png"
                                  &amp;&amp; imageindex.ContentType.ToLower() != "image/png")
                              {
                                  var qproid = db.Tbl_Product.OrderByDescending(a =&gt; a.Product_ID).FirstOrDefault();
                                  var qtagid = db.Tbl_Tags.Where(a =&gt; a.Tag_Proid == qproid.Product_ID).ToList();
  
                                  db.Tbl_Tags.RemoveRange(qtagid);
                                  db.Tbl_Product.Remove(qproid);
                                  db.SaveChanges();
  
                                  ViewBag.Error = " wrong format ";
                                  return View();
                              }
  
                              //-------------------------------------------
                              //  Check the image extension
                              //-------------------------------------------
                              if (Path.GetExtension(imageindex.FileName).ToLower() != ".jpg"
                                  &amp;&amp; Path.GetExtension(imageindex.FileName).ToLower() != ".png"
                                  &amp;&amp; Path.GetExtension(imageindex.FileName).ToLower() != ".gif"
                                  &amp;&amp; Path.GetExtension(imageindex.FileName).ToLower() != ".jpeg")
                              {
                                  var qproid = db.Tbl_Product.OrderByDescending(a =&gt; a.Product_ID).FirstOrDefault();
                                  var qtagid = db.Tbl_Tags.Where(a =&gt; a.Tag_Proid == qproid.Product_ID).ToList();
  
                                  db.Tbl_Tags.RemoveRange(qtagid);
                                  db.Tbl_Product.Remove(qproid);
                                  db.SaveChanges();
  
                                  ViewBag.Error = " wrong format ";
                                  return View();
                              }
  
  
                              if (item.ContentLength &lt;= 512000)
                              {
                                  Tbl_Pic pic = new Tbl_Pic();
                                  pic.Pic_Proid = db.Tbl_Product.OrderByDescending(a =&gt; a.Product_ID).FirstOrDefault().Product_ID;
                                  item.SaveAs(Path.Combine(Server.MapPath("~") + "/Content/_images/product/Gallery/" + namepic));
                                  var x = db.Tbl_Pic.OrderByDescending(a =&gt; a.Pic_ID).FirstOrDefault();
                                  if (x == null)
                                  {
                                      pic.Pic_ID = 1;
                                  }
                                  else
                                  {
                                      pic.Pic_ID = x.Pic_ID + 1;
                                  }
  
                                  pic.Pic_Name = namepic;
                                  lstpic.Add(pic);
                              }
                              else
                              {
                                  var qproid = db.Tbl_Product.OrderByDescending(a =&gt; a.Product_ID).FirstOrDefault();
                                  var qtagid = db.Tbl_Tags.Where(a =&gt; a.Tag_Proid == qproid.Product_ID).ToList();
  
                                  db.Tbl_Tags.RemoveRange(qtagid);
                                  db.Tbl_Product.Remove(qproid);
                                  db.SaveChanges();
  
                                  ViewBag.Error = "wrong size";
                                  return View();
                              }
                          }
  
                          db.Tbl_Pic.AddRange(lstpic);
                          db.SaveChanges();
                      }
                      else
                      {
                          var qproid = db.Tbl_Product.OrderByDescending(a =&gt; a.Product_ID).FirstOrDefault();
                          var qtagid = db.Tbl_Tags.Where(a =&gt; a.Tag_Proid == qproid.Product_ID).ToList();
  
                          db.Tbl_Tags.RemoveRange(qtagid);
                          db.Tbl_Product.Remove(qproid);
                          db.SaveChanges();
  
                          ViewBag.Error = "chose an image";
                          return View();
                      }
  
                      var qprorat = db.Tbl_Product.OrderByDescending(a =&gt; a.Product_ID).FirstOrDefault();
  
                      var iduser = db.Tbl_User.Where(a =&gt; a.User_ID == qprorat.Product_Userid).SingleOrDefault();
                      if (iduser != null)
                      {
                          iduser.User_Rating = iduser.User_Rating + 10;
  
                          db.Tbl_User.Attach(iduser);
                          db.Entry(iduser).State = System.Data.Entity.EntityState.Modified;
                          db.SaveChanges();
                      }
  
                      var qsmtp = db.Tbl_Setting.FirstOrDefault().Smtp;
                      var qemail = db.Tbl_Setting.FirstOrDefault().Email;
                      var qpassword = db.Tbl_Setting.FirstOrDefault().Password;
                      // TempData["OK"] ="added successfully";
                      TempData["Result"] = "OK";
  
                      Email email = new Email();
                      email.SendEmail(qsmtp, qemail, qpassword, quser.User_Email, "");
                      Tbl_Message m = new Tbl_Message();
                      m.Message_Body = "";
                      m.Message_Date = DateTime.Now;
                      m.Message_Read = false;
                      m.Message_Title = "new product added";
                      m.Message_UserGet = quser.User_ID;
                      db.Tbl_Message.Add(m);
                      db.SaveChanges();
                      return RedirectToAction("ListProduct");
                  }//end savchange
                  else
                  {
                      TempData["Result"] = "Error";
                      return RedirectToAction("ListProduct");
                  }
              }//end quser1
              else
              {
                  TempData["Result"] = "Error";
                  return RedirectToAction("ListProduct");
              }
          }//end quser
          else
          {
              TempData["State"] = "Error";
              return RedirectToAction("Profile", "Profile");
          }
      }//end role admin or seller
      else
      {
          TempData["State"] = "Error";
          return RedirectToAction("Profile", "Profile");
      }
  }
  </code></pre>