Kos
Kos

Reputation: 567

EF Code First - Fluent API - Many To Many relationship - not creating table

I have one entity and I want to create two Many-To-Many relationships. My entity is:

public class User : IdentityUser
{
    private ICollection<User> users;
    private ICollection<User> followers;
    private ICollection<User> followings;

    public User()
    {
        this.Users = new HashSet<User>();
        this.Followers = new HashSet<User>();
        this.Followings = new HashSet<User>();
    }

    public string FirstName { get; set; }

    public string LastName { get; set; }

    public virtual ICollection<User> Users
    {
        get { return this.users; }
        set { this.users = value; }
    }

    public virtual ICollection<User> Followers
    {
        get { return this.followers; }
        set { this.followers = value; }
    }

    public virtual ICollection<User> Followings
    {
        get { return this.followings; }
        set { this.followings = value; }
    }
}

My configuration class looks like this:

public class UserMap : EntityTypeConfiguration<User>
{
    public UserMap()
    {
        Property(u => u.FirstName).HasMaxLength(50).IsRequired();
        Property(u => u.LastName).HasMaxLength(50).IsRequired();

        HasMany<User>(u => u.Users).WithMany(f => f.Followers).Map(m =>
        {
            m.ToTable("UserFollower");
            m.MapLeftKey("UserId");
            m.MapRightKey("FollowerId");                
        });
        HasMany<User>(u => u.Users).WithMany(f => f.Followings).Map(m =>
        {
            m.ToTable("UserFollowing");
            m.MapLeftKey("UserId");
            m.MapRightKey("FollowingId");
        });
    }
}

This code creates only one table UserFollowing instead of two tables UserFollowing and UserFollower. If I swap the places of the tables then I am getting only UserFollower. It seems like the second table overwrite the first. I tried to delete the migrations and the database but I am still getting the same result.

Do anybody have idea why is that happening?

Upvotes: 0

Views: 500

Answers (1)

user894785
user894785

Reputation:

The problem you have here is that you're trying to create two M:N mappings against the same navigation property - Users. You actually don't need to do that though for what you're trying to achieve. To map which user is following which, you only need one mapping table.

Try setting it up like this:

    HasMany<User>(u => u.Following).WithMany(f => f.Followers).Map(m =>
    {
        m.ToTable("UserFollowing");
        m.MapLeftKey("UserId");
        m.MapRightKey("FollowingId");                
    });

Now, you can access Following on a User to find who they're following, and Followers to find out who's following them.

I suspect you don't actually need that Users property, at least for the purposes of data access. You could use a get only property to return the union of the Following and Followers collection though, if you want a list that combines both.

Upvotes: 1

Related Questions