Many-to-many relationship between entities of same type in MVC3

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?

Answers


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.

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.


Need Your Help

Asp.net Session expiring automatically after few seconds

asp.net session session-state session-timeout

When user login i am storing user_id in Session variable and on second page i am checking on page load if user_id exists then fine, otherwise redirect to sign in page but when i login and and redir...

can we open pdf using https URL using UIWebview

iphone objective-c xcode https

I'm making an application in which my application needs to using an https URLS.I want to display PDF File using https url in view(or)UIWebview. how do i do that? Please explain me ...Thanks in adv...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.