Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8263489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T04:07:19+00:00 2026-06-08T04:07:19+00:00

A post can have many Topics. A topic can be assigned to many posts.

  • 0

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. See Id=34 and 35. 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();
   }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T04:07:21+00:00Added an answer on June 8, 2026 at 4:07 am

    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();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have many Topic objects and each Topic hasMany posts:Post How can I order
I have two tables POSTS and COMMENTS. One post can have many comments and
I have tables: user, comment and post, say: A user can have many posts
I have two tables called 'events' and 'topics' each table can have many comments.
I have 2 many to many relationship tables; Posts and Categories. One post can
We have a website wherein a user can post information from our website to
A user can create a post. Posts have comments. A comment must belong to
I have a very simple question, How can I get the POST values from
In my project I have a Forum that has many Topics, and each Topic
I have the following set-up. BlogPosts BlogToCategory Category One blog post can have many

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.