问题描述
我很新的MVC,我有级联删除的麻烦。对于我的模型,我有以下2个类: public class Blog
{
[Key]
public int Id {get;组;
[必需]
public string Name {get;组; }
[DisplayFormat()]
public virtual ICollection< BlogEntry> BlogEntries {get;组; }
public DateTime CreationDateTime {get;组; }
public string UserName {get;组;
}
public class BlogEntry
{
[Key]
public int Id {get;组; }
[必需]
public string标题{get;组; }
[必需]
public string Summary {get;组; }
[必需]
public string Body {get;组; }
public List< Comment>评论{get;组; }
public List< Tag>标签{get;组; }
public DateTime CreationDateTime {get;组; }
public DateTime UpdateDateTime {get;组; }
public virtual Blog ParentBlog {get;组; }
}
对于我的控制器,我设置他在删除帖子后面返回:
[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);
}
问题是它不工作,无论什么; ,但我似乎只适用于关系是一对一的模型,所以我迷失在这里,我有搜索无处不在,找不到这个问题的解决方案,如果有人可以指出我错过了,真的很好:),谢谢提前,再次,原谅我的nooobness我刚刚开始,但想要解决一个大项目,以便能够学到很多。
这是因为EF默认情况下不强制执行级联删除用于可选关系。您的模型中的关系被推断为可选的。
您可以添加一个不可为空的标量属性( BlogId
) FK
public class BlogEntry
{
[Key]
public int Id {get ;组; }
[必需]
public string标题{get;组; }
[必需]
public string Summary {get;组; }
[必需]
public string Body {get;组; }
public List< Comment>评论{get;组; }
public List< Tag>标签{get;组; }
public DateTime CreationDateTime {get;组; }
public DateTime UpdateDateTime {get;组; }
public int ParentBlogId {get;组; }
public virtual Blog ParentBlog {get;组; }
}
或使用流畅的API配置此API
modelBuilder.Entity< BlogEntry>()
.HasRequired(b => b.ParentBlog)
.WithMany(b => b .BlogEntries)
.WillCascadeOnDelete(true);
I'm pretty new to MVC and i'm having troubles with cascade deleting. For my model I the following 2 classes:
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; }
}
And for my controller I set he following on delete post back:
[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");
}
Problem is it wont work no matter what; I read this post but i seem to only work for models where the relation is one to one, so i'm lost here, i have search everywhere, and can't find the solution for this problem, if some could point out what i'm missing it would be really nice :), thanks in advance, and again, pardon my nooobness i'm just getting started, but wanted to tackle a big project to be able to learn a lot.
That is because EF by default does not enforce cascade deletes for optional relationships. The relationship in your model is inferred as optional.
You can add a non nullable scalar property(BlogId
) of the FK
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 int ParentBlogId { get; set; }
public virtual Blog ParentBlog { get; set; }
}
Or configure this using fluent API
modelBuilder.Entity<BlogEntry>()
.HasRequired(b => b.ParentBlog)
.WithMany(b => b.BlogEntries)
.WillCascadeOnDelete(true);
这篇关于使用EF Code First Approach时,MVC .Net Cascade删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!