EF代码第一:将多对多 [英] EF Code first: Insert Many to many
问题描述
一个岗位可以有很多话题。一个主题可以分配给许多职位。
当添加与主题列表中选择两个主题后,两个 NULL
主题还插入我演讲的题目表。参见 n = 34
和 35
。我做错了什么?话题不应该被改变。我加入一个新的岗位,并从固定数量的主题(一个DropDownList)选题。它是一直跟踪在PostTopics表(帖子ID,TopicID)
主题表:
标识TopicName TopicDesc
31体育体育
32游戏游戏
33政治政治
34 NULL NULL
35 NULL NULL
块引用>
TopicPosts表:
Topic_Id POST_ID
34 11
35 11
公共类邮报
{
公众诠释标识{搞定;组; }
公众诠释用户ID {搞定;组; }
公共虚拟的ICollection<&主题GT; PostTopics {搞定;组; }
}
公共类主题
{
公众诠释标识{搞定;组; }
公共字符串TopicName {搞定;组; }
公共虚拟的ICollection<请求>请求{搞定;组; }
}
//插入代码:我认为这个问题是在这里使用
(VAR背景=新ChatContext())
{
//邮报
context.Posts.Add(pobjPost);
pobjPost.PostTopics =新的List<&主题GT;();
//主题
的foreach(变种i的pobjTopics)
{
pobjPost.PostTopics.Add(ⅰ);
}
context.SaveChanges();
}
解决方案您必须首先将话题上下文将它们放到状态
不变
,并告诉他们已经在数据库中现有的EF。否则,EF会认为今天的题目是新的,它们插入到数据库中。之后,你可以后添加到背景下,这样的职位可以连同关系记录在许多一对多连接表插入一个新的实体到数据库:使用(VAR上下文=新ChatContext())
{
pobjPost.PostTopics =新的List<&主题GT;();
的foreach(在pobjTopics VAR pobjTopic)
{
context.Topics.Attach(pobjTopic); //话题是状态不变,现在
pobjPost.PostTopics.Add(pobjTopic);
}
context.Posts.Add(pobjPost); //在后添加的状态,主题依然不变
context.SaveChanges();
}
A post can have many Topics. A topic can be assigned to many posts. When adding a post with two topics selected from topic list, two
NULL
topics also inserted to my topic table. SeeId=34
and35
. What did I do wrong? Topics should not be changed. I am adding a new post and selecting topics from fixed number of topics (a dropdownlist). It is keep tracked in PostTopics table (PostID, TopicID).Topics table:
Id TopicName TopicDesc 31 Sports Sports 32 Game Game 33 Politics Politics 34 NULL NULL 35 NULL NULL
TopicPosts table:
Topic_Id Post_Id 34 11 35 11 public class Post { public int Id { get; set; } public int UserId { get; set; } public virtual ICollection<Topic> PostTopics { get; set; } } public class Topic { public int Id { get; set; } public string TopicName { get; set; } public virtual ICollection<Request> Requests { get; set; } } // insert code: I think the problem is here using (var context = new ChatContext()) { // Post context.Posts.Add(pobjPost); pobjPost.PostTopics = new List<Topic>(); // topics foreach (var i in pobjTopics) { pobjPost.PostTopics.Add(i); } context.SaveChanges(); }
解决方案You must attach the topics first to the context to put them into state
Unchanged
and tell EF that they are already existing in the database. Otherwise EF will assume that the topics are new and insert them into the database. After that you can add the post to the context so that the post can be inserted as a new entity into the database together with the relationship records in the many-to-many join table:using (var context = new ChatContext()) { pobjPost.PostTopics = new List<Topic>(); foreach (var pobjTopic in pobjTopics) { context.Topics.Attach(pobjTopic); // topic is in state Unchanged now pobjPost.PostTopics.Add(pobjTopic); } context.Posts.Add(pobjPost); // post in state Added, topics still Unchanged context.SaveChanges(); }
这篇关于EF代码第一:将多对多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!