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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T04:02:33+00:00 2026-06-03T04:02:33+00:00

I have an ASP.NET MVC3 application, where I use Entity Framework 4.3 Code-First and

  • 0

I have an ASP.NET MVC3 application, where I use Entity Framework 4.3 Code-First and Migrations.

I’ve been trying to create a many-to-many relationship between entities of the same type, but when I scaffold the migration with Migrations, it generates a one-to-one relationship.

The idea is that one user should be able to follow multiple other users (think Twitter).

My User model look something like this:

public class User
{
    public int UserId { get; set; }
    public string Name { get; set; }
    public DateTime Registered { get; set; }
    ...
    public virtual ICollection<User> Follows { get; set; }
}

When I scaffold the the added Follows-property, I get a migration like this:

public partial class FollowUser : DbMigration
{
    public override void Up()
    {
        AddColumn("User", "User_UserId", c => c.Int());
        AddForeignKey("User", "User_UserId", "User", "UserId");
        CreateIndex("User", "User_UserId");
    }

    ...
}

So Entity Framework interprets my model as a one-to-one relationship between two users.

How can I create a many-to-many relationship between entities of the same type?

  • 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-03T04:02:34+00:00Added an answer on June 3, 2026 at 4:02 am

    Since It is a Many to Many Self Joined relationship, User entity should have a Followers and Following properties both of Type User. So Let us alter your class to include those

    public class User
    {
        public int ID { get; set; }
        public string Name { get; set; }       
        public virtual ICollection<User> Followers { get; set; }
        public virtual ICollection<User> Following { get; set; }
    }
    

    Now update the DbContext class to have a Property which returns the Users

     public DbSet<User> Users { set; get; }
    

    We need to tell EntityFramework that we need to have a many to many relations ship between these two. So Let us do that with Fluent API by overriding OnModelCreating method

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<User>().HasMany(m => m.Followers).WithMany(p => p.Following).Map(w => w.ToTable("User_Follow").MapLeftKey("UserId").MapRightKey("FollowerID"));
        base.OnModelCreating(modelBuilder);
    }
    

    Now Entity Framework will create the tables like this.

    enter image description here

    Note that we explicitly told Entity Framework to use UserId and FollowerId as the Forign Key column name. If we havent mentioned that, EF will use User_ID and User_ID1 as the column names.

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

Sidebar

Related Questions

I have my asp.net mvc 3 application with entity framework and i used the
I am building an asp.net mvc3 application and I have decided to use areas
In my ASP.NET MVC3 application I have the following method that use EF entities
I have a ASP.NET MVC3 application that handles time-consuming processes (copying a large file
I am creating an ASP.NET MVC3 application using NHIBERNATE. I have a base class
This is about asp.net mvc3 web application. We have used Object cache to store
I have an ASP.NET MVC 3 application which has a post action called Create
Well, this is an interesting problem. I have an ASP.NET MVC3 Intranet application running
I have an ASP.NET MVC3 in C# and Razor. The architecture of the application
I have an asp.net MVC3 application that makes Ajax calls to the server on

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.