EF Code First 建立一对一关系而不是多对多关系

EF Code First makes One-To-One instead of Many-To-Many relationship

我注意到我的 EF Code First fluent API 映射有一个奇怪的问题。我想建立两个多对多关系,但 EF 似乎没有看到我重载的 OnModelCreating 函数,按照自己的约定建立一个 table 和另一个 converts to One-To-One relation.

型号:

public partial class Task
{
    public Task()
    {
        ParentTasks = new HashSet<Task>();
        ChildTasks = new HashSet<Task>();
    }
    public int Id { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
    public byte Progress { get; set; }
    public int Priority { get; set; }
    public byte[] Attachment { get; set; }
    public string Url { get; set; }
    public DateTime CreateDate { get; set; }
    public DateTime? Deadline { get; set; }

    //Foreign keys
    public int ListId { get; set; }

    //Navigation properties
    public TodoList List { get; set; }
    public ICollection<Task> ParentTasks { get; set; }
    public ICollection<Task> ChildTasks { get; set; }
}

public partial class TodoList
{
    public TodoList()
    {
        Tasks = new HashSet<Task>();
        Users = new HashSet<ApplicationUser>();
    }
    public int Id { get; set; }
    [MaxLength(250)]
    public string ListName { get; set; }

    //Foreign keys
    public int? GroupId { get; set; }
    [MaxLength(128)]
    public string OwnerId { get; set; }

    //Navigation properties
    public ApplicationUser Owner { get; set; }
    public Group Group { get; set; }
    public ICollection<Task> Tasks { get; set; }
    /// <summary>
    /// List of the users that have this list shared.
    /// </summary>
    public ICollection<ApplicationUser> Users { get; set; }
}

public class ApplicationUser : IdentityUser
{
    public ApplicationUser()
    {
        TodoLists = new HashSet<TodoList>();
    }
    [StringLength(100, ErrorMessage = "The {0} must be at least {2} and at most {1} characters long.", MinimumLength = 2)]
    [Display(Name = "Name")]
    public string FirstName { get; set; }
    [StringLength(100, ErrorMessage = "The {0} must be at least {2} and at most {1} characters long.", MinimumLength = 2)]
    [Display(Name = "Surname")]
    public string LastName { get; set; }
    [MaxLength(1024*1024, ErrorMessage = "Max size: 1MB")]
    public byte[] Avatar { get; set; }

    //Navigation properties

    /// <summary>
    /// List of the shared todo lists with specific user.
    /// </summary>
    public ICollection<TodoList> TodoLists { get; set; }
}

和 DbContext:

public class TodoDbContext : DbContext
{
    public TodoDbContext() : base("DefaultConnection")
    {
    }

    public DbSet<Task> Tasks { get; set; }
    public DbSet<TodoList> TodoLists { get; set; }
    public DbSet<Group> Groups { get; set; }
    public DbSet<ApplicationUser> Users { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Task>()
            .HasMany<Task>(t => t.ParentTasks)
            .WithMany(t => t.ChildTasks)
            .Map(m =>
                m.MapLeftKey("TaskID")
                .MapRightKey("ParentID")
                .ToTable("TaskCorrelations")
            );

        modelBuilder.Entity<TodoList>()
            .HasMany<ApplicationUser>(t => t.Users)
            .WithMany(a => a.TodoLists)
            .Map(m =>
                m.MapLeftKey("ListID")
                .MapRightKey("UserID")
                .ToTable("SharedTodoLists"));
    }

    public static TodoDbContext Create()
    {
        return new TodoDbContext();
    }
}

我需要手动 "run" 这个上下文吗? EF 迁移系统甚至看不到我的 DbContext 中的更改。

编辑:这些是我用配置做的事情:

您应该像这样配置迁移:

TodoDbContext

public class TodoDbContext : ApplicationDbContext
{
    /...
}

配置

internal sealed class Configuration : DbMigrationConfiguration<TodoDbContext>
{
    public Configuration()
    {
        // ...
    }

    protected override void Seed(TodoDbContext context)
    {
        // ...
    }
}