我在我的 MVC 3 应用程序中使用实体框架 4.3,当尝试更新实体时(创建和删除工作正常)我收到此错误:
存储更新、插入或删除语句影响了意外数量的行 (0)
当我进入调试模式时,我看到在 [HttpPost] 方法上没有提供提要 ID:
public ActionResult Edit(Feed feed)
{
if (ModelState.IsValid)
{
db.Entry(feed).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.FolderId = new SelectList(db.Folders, "FolderId", "Name", feed.FolderId);
return View(feed);
}
尽管在普通的 Get 方法中传递了 id。这些是我的实体
喂养:
public class Feed
{
[ScaffoldColumn(false)]
public int FeedId { get; set; }
[StringLength(150, ErrorMessage = "The {0} must be less then {1} charecters")]
public string Title { get; set; }
[ScaffoldColumn(false)]
public string Description { get; set; }
[Required(ErrorMessage = "you must enter a valid link")]
[StringLength(500, ErrorMessage = "The {0} must be less then {1} characters long.")]
public string LinkUrl { get; set; }
[ScaffoldColumn(false)]
public DateTime PublishDate { get; set; }
[ScaffoldColumn(false)]
public string Image { get; set; }
[ForeignKey("Folder")]
[Required(ErrorMessage="you must choose a folder")]
public int FolderId { get; set; }
public virtual Folder Folder { get; set; }
public Feed()
{
PublishDate = new DateTime(2012, 1, 1);
}
}
文件夹:
public class Folder
{
public int FolderId { get; set; }
[Required(ErrorMessage = "you must enter a folder name")]
[StringLength(150, ErrorMessage = "the {0} must be less then {1} charecters")]
public string Name { get; set; }
}
我一直在寻找解决方案,但它们都不起作用,例如尝试刷新方法,该方法在 DbContext 中不存在,或者在 FeedId 和 FolderId 上方定义 [Key] 属性。