C# EF 6.0多对多关系编辑
我的EntityFramework6.0有一个问题,我的设置如下C# EF 6.0多对多关系编辑,c#,asp.net-mvc,entity-framework,C#,Asp.net Mvc,Entity Framework,我的EntityFramework6.0有一个问题,我的设置如下 public Post { [Key] public int Id {get;set;} public String Name {get;set;} public virtual List<Category> Categories {get;set;} } public Category { [Key] public int Id {get;set;} p
public Post
{
[Key]
public int Id {get;set;}
public String Name {get;set;}
public virtual List<Category> Categories {get;set;}
}
public Category
{
[Key]
public int Id {get;set;}
public string Name {get;set;}
public virtual List<Post> Posts {get;set;}
}
我得到了以下异常,只有当我试图修改一个已经创建并且具有类别的帖子时才会发生异常
如果外键不支持空值,则必须定义新的关系,必须为外键属性分配另一个非空值,或者必须删除不相关的对象。
我不太确定在这种情况下该怎么办,我是否也应该从类别中删除帖子???请记住,通过从列表中删除类别,我只想将其从该集合中删除,而不是从数据库中删除整个对象。有什么建议吗
这是我第一次向StackOverflow发帖,如果有人需要更多信息,请告诉我。我处理这些多对多关系的方法如下:(假设帖子是数据库中的对象) 在删除后提交更改时效果最佳。 如果没有更改,或者如果删除并再次添加同一实体而不提交,则可能会抛出一些错误
我相信可能会有更好的解决方案。您可以定义中间表,然后删除其中的记录。这样,您就不会删除类别本身,而现在您似乎正在这样做。我建议您对模型进行如下修改:
public Post
{
[Key]
public int Id {get;set;}
public String Name {get;set;}
//For the many to 1 relationship
public virtual ICollection <PostCategory> PostCategories{get;set;}
//You wont need this anymore
//public virtual List<Category> Categories {get;set;}
}
还记得在dbcontext中定义“后分类”。(我想你知道怎么…?)
现在,当您想要删除帖子和类别之间的链接时,只需删除PostCategory记录,就像控制器中的这样:
//Find the record where postId is the PostId and the categoryId is the CategoryId
var postRecord = db.PostCategories.FirstOrDefault(x=>x.PostId==postId && x.CategoryId==categoryId);
if(postRecord!=null)
{
db.PostCategories.Remove(postRecord)
db.SaveChanges();
}
添加记录也很容易。我在控制器中这样做
//First create a record to add
PostCategory pc= new PostCategory()
//wire it up... EF adds the Id fields into the record. If you have a problem
// you can even add those.
pc.Category = category;
pc.Post = post;
//add it
db.PostCategories.Add(pc);
db.SaveChanges();
我喜欢这种方法,因为现在你可以在你的PostCategory表中保存额外的内容,如发布日期等。我不喜欢多对多的关系,我相信它们迟早会分解为一对多和多对一。。。后来,当你不得不“修复代码”时——至少可以说,这是一种痛苦。我希望这有帮助。什么是
帖子
?什么是类别
?你需要展示所有这些:)虽然这会起作用,但它似乎放弃了EF在多对多关系中提供的抽象。是的,我相信这是积极的一面。此外,在将来的某个地方,需要将信息添加到中间表中,使用此方法可以使用该选项。每个人都有自己的毒药。
public Category
{
[Key]
public int Id {get;set;}
public string Name {get;set;}
//For the many to 1 relationship
public virtual ICollection <PostCategory> PostCategories{get;set;}
//You wont need this anymore
//public virtual List<Post> Posts {get;set;}
}
public PostCategory
{
[Key]
public int Id {get;set;}
public int PostId {get;set;}
public virtual Post Post {get;set;}
public int CategoryId {get;set;}
public virtual Category Category {get;set;}
}
//Find the record where postId is the PostId and the categoryId is the CategoryId
var postRecord = db.PostCategories.FirstOrDefault(x=>x.PostId==postId && x.CategoryId==categoryId);
if(postRecord!=null)
{
db.PostCategories.Remove(postRecord)
db.SaveChanges();
}
//First create a record to add
PostCategory pc= new PostCategory()
//wire it up... EF adds the Id fields into the record. If you have a problem
// you can even add those.
pc.Category = category;
pc.Post = post;
//add it
db.PostCategories.Add(pc);
db.SaveChanges();