简体   繁体   English

MVC3中相同类型的实体之间的多对多关系

[英]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. 我有一个ASP.NET MVC3应用程序,我使用Entity Framework 4.3 Code-First和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). 这个想法是一个用户应该能够跟随多个其他用户(想想Twitter)。

My User model look something like this: 我的User模型看起来像这样:

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: 当我支持添加的Follows-property时,我得到这样的迁移:

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? 如何在相同类型的实体之间创建多对多关系?

Since It is a Many to Many Self Joined relationship, User entity should have a Followers and Following properties both of Type User . 由于这是一个多对多的自我加入的关系,用户实体应该有一个FollowersFollowing的两种类型属性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 现在更新DbContext类以具有返回Users的Property

 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. 我们需要告诉EntityFramework我们需要在这两者之间存在多对多的关系。 So Let us do that with Fluent API by overriding OnModelCreating method 因此,让我们通过重写OnModelCreating方法,使用Fluent API来实现这OnModelCreating

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. 现在Entity Framework将创建这样的表。

在此输入图像描述

Note that we explicitly told Entity Framework to use UserId and FollowerId as the Forign Key column name. 请注意,我们明确告知Entity Framework使用UserIdFollowerId作为Forign Key列名。 If we havent mentioned that, EF will use User_ID and User_ID1 as the column names. 如果我们没有提到,EF将使用User_IDUser_ID1作为列名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM