我对MVC非常陌生,并且遇到了级联删除的麻烦。对于我的I型以下2类:使用EF Code First Approach时MVC .Net Cascade删除
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但我似乎只适用于模型的关系是一对一的,所以我迷失在这里,我到处搜索,并找不到解决方案,如果有人能指出我缺少的东西这将是非常好的:),在此先感谢,并再次,原谅我的nooobness我刚刚开始,但想要解决一个大项目,能够学到很多东西。
耶! :D,谢谢它的工作!,非常感谢,我只是不知道流利的API,但似乎是一个很好的方式来做这件事情,顺便说一句,我可以得到这种行为与数据注释?。 – 2012-02-11 16:44:47
@Q_ro数据注释具有有限的一组功能。 – Eranga 2012-02-12 00:43:02