简体   繁体   English

EF代码优先:插入多对多

[英]EF Code first: Insert Many to many

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. 添加从主题列表中选择的两个主题的帖子时,还会将两个NULL主题插入到我的主题表中。 See Id=34 and 35 . Id=3435 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). 它在PostTopics表(PostID,TopicID)中被跟踪。

Topics table: 主题表:

 Id TopicName TopicDesc 31 Sports Sports 32 Game Game 33 Politics Politics 34 NULL NULL 35 NULL NULL 

TopicPosts table: TopicPosts表:

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. 您必须首先将主题附加到上下文,以使它们处于未Unchanged状态,并告诉EF它们已存在于数据库中。 Otherwise EF will assume that the topics are new and insert them into the database. 否则,EF将假定主题是新主题并将其插入数据库。 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();
}

To create a relationship, both relating objects should be attached into the context first. 要创建关系,应首先将两个相关对象附加到上下文中。

Try this way: 试试这种方式:

using (var context = new ChatContext())
      {
                 // Post
                 context.Posts.Attach(pobjPost);

                 pobjPost.PostTopics = new List<Topic>();
                 // topics
                 foreach (var i in pobjTopics)
                 {

                 pobjPost.PostTopics.Add(i);
                 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM