【发布时间】:2012-03-03 17:51:38
【问题描述】:
我对 MVC 很陌生,但在级联删除方面遇到了麻烦。对于我的模型,我有以下 2 个类:
public class Blog
{
[Key]
public int Id { get; set; }
[Required]
public string Name { get; set; }
[DisplayFormat()]
public virtual ICollection<BlogEntry> BlogEntries { get; set; }
public DateTime CreationDateTime { get; set; }
public string UserName { get; set; }
}
public class BlogEntry
{
[Key]
public int Id { get; set; }
[Required]
public string Title { get; set; }
[Required]
public string Summary { get; set; }
[Required]
public string Body { get; set; }
public List<Comment> Comments { get; set; }
public List<Tag> Tags { get; set; }
public DateTime CreationDateTime { get; set; }
public DateTime UpdateDateTime { get; set; }
public virtual Blog ParentBlog { get; set; }
}
对于我的控制器,我将他设置为删除回发:
[HttpPost, ActionName("Delete")]
public ActionResult DeleteConfirmed(int id)
{
Blag blog = db.Blogs.Find(id);
foreach (var blogentry in blog.BlogEntries)
{
//blogentry = db.BlogEntries.Find(id);
db.BlogEntries.Remove(blogentry);
}
db.Blogs.Remove(blog);
db.SaveChanges();
return RedirectToAction("Index");
}
问题是无论如何它都行不通; I read this post 但我似乎只适用于关系是一对一的模型,所以我在这里迷路了,我到处搜索,找不到这个问题的解决方案,如果有人能指出我的意思的话我错过它会非常好:),在此先感谢,再一次,请原谅我的菜鸟,我才刚刚开始,但想解决一个大项目以便能够学到很多东西。
【问题讨论】:
标签: .net asp.net-mvc entity-framework-4.1 ef-code-first