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 8361365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:40:55+00:00 2026-06-09T11:40:55+00:00

I have a many to many relationship. a tag can have multiple articles and

  • 0

I have a many to many relationship.

a tag can have multiple articles and an article can have multiply tags.

public class Article : Entity
{
    [StringLength(150)]
    public string Name { get; set; } 
    public string Content { get; set; } 
    public ICollection<Tag> Tags { get; set; }
}
public class Tag : Entity
{
    public string Name { get; set; }
    public ICollection<Article> Articles { get; set; }
}

the ef codefirst creates a table called TagArticles with columns Tag_Id and Article_Id

I tried to seed like this

    protected override void Seed(Context context)
    {
        var articles = new List<Article>();
        for (int i = 0; i < 10; i++)
        {
            articles.Add(new Article { Name = "test " + i,  Content = "lourm ipsum"  });
        }
        var tags = new List<Tag>();
        for (int i = 0; i < 10; i++)
        {
            tags.Add(new Tag { Name = "tag " + i , Articles = new  Article[]{ articles[0] }});
        }
        context.Articles.AddOrUpdate(x => x.Name, articles.ToArray());
        context.Tags.AddOrUpdate(x => x.Name, tags.ToArray());
    }

but the TagArticles table is empty after the migration, how do I seed the bridge table?

  • 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-09T11:40:57+00:00Added an answer on June 9, 2026 at 11:40 am

    The code you had above worked for me when I put it in, but I had to add the Primary Keys in order for the database to actually save.

    Where are you setting the primary keys? Is that in the Entity inheritance?

    After you seed, are there Articles and Tags in the database (there wasn’t for me until I corrected the PK issue)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a many-to-many relationship between photos and tags: A photo can have multiple
I've got two tables (articles and tags) that have a one-to-many relationship. I remember
Using CoreData, I have an entity Bookmark, that has an to-many relationship named 'tags'
I have two models, Purchase and Tag . A Purchase can have many Tags
I have 2 Entities, related by a many-to-many relationship. Thing<<->>Tag There is one NSArrayController
I have many to many relationship in entity framework. See image... I want to
I have entities Word and Tag . Both have To-Many relationship to each other,
I have entities Post and Tag , where there is a many-to-many relationship between
Lets say I have 3 tables Posts, PostTags and Tags defining a many-to-many relationship.
I have a 3 tables which are News, Tags, News_Tags with Many-To-Many relationship. With

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.