title: Iterate through model and update values (ASP.NET MVC3)
body: <p>I have a site where I want to remove all ads that are older than 15 days. I don't want to remove the posts from the db, but only flag them as "deleted = true". Im trying to update the values while iterating through the list of ads, but without success. Here is my code:</p>
  
  <pre><code>    [Authorize]
      [ValidateAntiForgeryToken]
      [HttpPost]
      public ActionResult RemoveOldAdsConfirmed()
      {
          DateTime removeDate = DateTime.Now.AddDays(-15);
          var removeAds = db.Ads.Where(a =&gt; a.Date &lt;= removeDate &amp;&amp; a.Deleted != true).OrderByDescending(a =&gt; a.Date);
  
          foreach (var items in removeAds)
          {
              // remove the images related to that specific ad
              foreach (var item in items.Images.ToList())
              {
                  // remove image from disk
                  System.IO.File.Delete(Server.MapPath("~/Content/img/UserUploads/") + item.ImageUrl);
                  System.IO.File.Delete(Server.MapPath("~/Content/img/UserUploads/") + "medium_" + item.ImageUrl);
                  System.IO.File.Delete(Server.MapPath("~/Content/img/UserUploads/") + "small_" + item.ImageUrl);
  
                  // remove image from db
                  Image deleteImage = db.Images.Find(item.ImageId);
                  db.Images.Remove(deleteImage);
                  db.SaveChanges();
              }
  
              // mark the ad as "Deleted"
              items.Deleted = true;
  
              // update the db
              db.Entry(items).State = EntityState.Modified;
              db.SaveChanges();
          }
          // Redirect to view that lists ads that should be removed
          return RedirectToAction("RemoveOldAds");
      }
  </code></pre>
  
  <p>This is my model:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.ComponentModel.DataAnnotations;
  using System.Web.Mvc;
  
  namespace AlltForMusik.Models
  {
  public class Ad
  {
      public virtual int AdId { get; set; }
  
      public virtual int CategoryId { get; set; }
  
      [Required(ErrorMessage = "Obligatoriskt fält")]
      [StringLength(70, ErrorMessage = "Max 70 tecken")]
      [Display(Name="Rubrik")]
      public virtual string Headline { get; set; }
  
      [Required(ErrorMessage = "Obligatoriskt fält")]
      [Display(Name = "Beskrivning")]
      [DataType(DataType.MultilineText)]
      public virtual string Text { get; set; }
  
      [ScaffoldColumn(false)]
      public virtual DateTime Date { get; set; }
  
      [Required(ErrorMessage = "Obligatoriskt fält")]
      [Display(Name = "Pris")]
      [Range(0, 999999999)]
      public virtual int Price { get; set; }
  
      [Display(Name = "Kan skickas")]
      public virtual int KanSkickas { get; set; }
  
      [Display(Name = "BVSA")]
      public virtual int BVSA { get; set; }
  
      [Required(ErrorMessage = "Obligatoriskt fält")]
      [StringLength(40, ErrorMessage = "Max 40 tecken")]
      [Display(Name = "Ditt namn")]
      public virtual string Name { get; set; }
  
      [StringLength(15, ErrorMessage = "Max 15 tecken")]
      [Display(Name = "Telefon")]
      public virtual string Phone { get; set; }
  
      [Required(ErrorMessage = "Obligatoriskt fält")]
      [StringLength(100, ErrorMessage = "Max 100 tecken")]
      [DataType(DataType.EmailAddress)]
      [RegularExpression(@"^([\w\!\#$\%\&amp;\'\*\+\-\/\=\?\^\`{\|\}\~]+\.)*[\w\!\#$\%\&amp;\'\*\+\-\/\=\?\^\`{\|\}\~]+@((((([a-zA-Z0-9]{1}[a-zA-Z0-9\-]{0,62}[a-zA-Z0-9]{1})|[a-zA-Z])\.)+[a-zA-Z]{2,6})|(\d{1,3}\.){3}\d{1,3}(\:\d{1,5})?)$",
                                      ErrorMessage="Inte en giltig e-postadress")]
      [Display(Name = "E-post")]
      public virtual string Email { get; set; }
  
      [Required(ErrorMessage = "Obligatoriskt fält")]
      [Display(Name = "Län")]
      public virtual int CountyId { get; set; }
  
      [Required(ErrorMessage = "Obligatoriskt fält")]
      [Display(Name = "Stad")]
      public virtual int CityId { get; set; }
  
      [Required(ErrorMessage = "Obligatoriskt fält")]
      public virtual int Type { get; set; }
  
      public string Password { get; set; }
  
      public bool Deleted { get; set; }
  
      [Display(Name = "Kategori")]
      public virtual Category Category { get; set; }
  
      public virtual ICollection&lt;Image&gt; Images { get; set; }
  
  
  }
  }
  </code></pre>
  
  <p>Im getting this error message:</p>
  
  <p>Exception Details: </p>
  
  <blockquote>
    <p>System.Data.Entity.Validation.DbEntityValidationException:<br>
    Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.</p>
  </blockquote>
  
  <p>What is the problem?</p>
  
  <p>Best regards
  Mikael</p>