使用EF code第一种方式时MVC净级联删除

编程入门 行业动态 更新时间:2024-10-27 22:25:43
本文介绍了使用EF code第一种方式时MVC净级联删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我是pretty的新MVC和我遇到的麻烦与级联删除。对于我的模型我以下2类:

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"); }

问题是,它不会工作,不管是什么; I阅读这篇文章但我似乎只工作模式,其中的关系是一一对应的,所以我在这里失去了,我有搜索无处不在,找不到这个问题的解决方案,如果有的可以点什么我失踪那将是非常好的:),在此先感谢,并再次,原谅我nooobness我刚开始起步,但要解决的一个大项目,以便能够学到很多东西。

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.

推荐答案

这是因为EF在默认情况下不强制执行级联删除可选的关系。在模型中的关系推断为可选。

That is because EF by default does not enforce cascade deletes for optional relationships. The relationship in your model is inferred as optional.

您可以添加一个不为空的标量属性(BlogId )的FK的

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; } }

或配置此用流利的API

Or configure this using fluent API

modelBuilder.Entity<BlogEntry>() .HasRequired(b => b.ParentBlog) .WithMany(b => b.BlogEntries) .WillCascadeOnDelete(true);

更多推荐

使用EF code第一种方式时MVC净级联删除

本文发布于:2023-05-29 23:14:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/355170.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:第一种   级联   方式   EF   code

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!