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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:33:20+00:00 2026-05-25T13:33:20+00:00

i am having a four models.. User,post,tag,comment.. the relationship i want to have with

  • 0

i am having a four models.. User,post,tag,comment.. the relationship i want to have with EF code first is

1) 1 user can have many posts, can create many tags, can have many comments
2) 1 post can have 1 user, many comments, and can have many tags
3) 1 tag can have 1 user, many post
4) 1 comment can have 1 user, 1 post

through google, i have come this far …but i am sure its not right …please help..

Updated Mapping

 modelBuilder.Entity<User>().HasMany(p => p.Posts).WithRequired(u => u.Users);
        modelBuilder.Entity<User>().HasMany(t => t.Tags).WithRequired(u => u.Users);
        modelBuilder.Entity<User>().HasMany(c => c.Comments).WithRequired(u => u.Users);

        modelBuilder.Entity<Post>().HasRequired(u => u.Users);
        modelBuilder.Entity<Post>().HasMany(t => t.Tags).WithMany(p => p.Posts);
        modelBuilder.Entity<Post>().HasMany(c => c.Comments);

        modelBuilder.Entity<Comment>().HasRequired(u => u.Users);
        modelBuilder.Entity<Comment>().HasRequired(p => p.Posts);

        modelBuilder.Entity<Tag>().HasRequired(u => u.Users);
        modelBuilder.Entity<Tag>().HasMany(p => p.Posts).WithMany(t => t.Tags);

please help.

  • 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-05-25T13:33:21+00:00Added an answer on May 25, 2026 at 1:33 pm

    If one User can have many Posts and Post can have single User your entities and mappings must be prepared for that.

    public class User 
    {
        ...
        public virtual ICollection<Post> Posts { get; set; }
    }
    
    public class Post
    {
        ...
        public virtual User { get; set;}
        public virtual ICollection<Tag> { get; set; }
    }
    
    public class Tag
    {
        ...
        public virtual ICollection<Post> Posts { get; set; }
    }
    

    If you define your entities correctly it should make correct mapping for you with default conventions. If you still to map it with fluent API you must use:

    modelBuilder.Entity<User>().HasMany(u => u.Posts).WithRequired(p => p.User);
    

    Which means one user can have many posts and post must have single user. That is 1-N mapping. For tag and post you need M-N mapping:

    modelBuilder.Entity<Tag>().HasMany(t => t.Posts).WithMany(p => p.Tags);
    

    Which means one tag can have many posts and post can have many tags.

    You will map other relations in the same way.

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

Sidebar

Related Questions

I'm having problems getting my brain around this. I have four models: Account has
I have four Excel sheets where i am having Movies list. i want to
I have a hashmap having four values a , b , c , d
i have one datagridview.in my datagridview is having four row and three columns.after enter
Having just read the first four chapters of Refactoring: Improving the Design of Existing
I want to write a method having four input parameters,all the input parameters are
Assume i have four classes.Each class is having a button to do screen switching
I have four models with the relationships Model PagetTemplate(models.Model): pass Model TextKey(models.Model): page_template =
I have four machines: XP with SQL 2000 (Machine A, the one I'm having
I'm having trouble with grep.. Which four patterns should I use with PHP's preg_grep

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.