繁体   English   中英

EF代码优先:插入多对多

[英]EF Code first: Insert Many to many

帖子可以有很多主题。 可以将主题分配给许多帖子。 添加从主题列表中选择的两个主题的帖子时,还会将两个NULL主题插入到我的主题表中。 Id=3435 我做错了什么? 不应该改变主题。 我正在添加一个新帖子并从固定数量的主题中选择主题(下拉列表)。 它在PostTopics表(PostID,TopicID)中被跟踪。

主题表:

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

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

您必须首先将主题附加到上下文,以使它们处于未Unchanged状态,并告诉EF它们已存在于数据库中。 否则,EF将假定主题是新主题并将其插入数据库。 之后,您可以将帖子添加到上下文中,以便可以将帖子作为新实体与多对多连接表中的关系记录一起插入到数据库中:

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

要创建关系,应首先将两个相关对象附加到上下文中。

试试这种方式:

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