我有一个网站,我想删除所有超过 15 天的广告。我不想从数据库中删除帖子,而只是将它们标记为“已删除 = true”。我试图在遍历广告列表时更新值,但没有成功。这是我的代码:
[Authorize]
[ValidateAntiForgeryToken]
[HttpPost]
public ActionResult RemoveOldAdsConfirmed()
{
DateTime removeDate = DateTime.Now.AddDays(-15);
var removeAds = db.Ads.Where(a => a.Date <= removeDate && a.Deleted != true).OrderByDescending(a => 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");
}
这是我的模型:
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\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+\.)*[\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+@((((([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<Image> Images { get; set; }
}
}
我收到此错误消息:
异常详情:
System.Data.Entity.Validation.DbEntityValidationException:
一个或多个实体的验证失败。有关更多详细信息,请参阅“EntityValidationErrors”属性。
问题是什么?
最好的问候米凯尔