C# EF代码第一次多对多迁移忽略链接表规范

C# EF代码第一次多对多迁移忽略链接表规范,c#,entity-framework,ef-code-first,entity-framework-migrations,C#,Entity Framework,Ef Code First,Entity Framework Migrations,我试图在我的db模型中的两个表之间添加一个新的多对多关系。生成的迁移忽略了我指定的链接表,而是在左侧表中添加了一个外键。我怎样才能解决这个问题 我在其他多对多关系中使用了相同的规范格式,效果良好 public class Competitor { [Key] public Guid ID { get; set; } public string FirstName { get; set; } public string LastName { get; set; }

我试图在我的db模型中的两个表之间添加一个新的多对多关系。生成的迁移忽略了我指定的链接表,而是在左侧表中添加了一个外键。我怎样才能解决这个问题

我在其他多对多关系中使用了相同的规范格式,效果良好

public class Competitor
{
    [Key]
    public Guid ID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string FullName { get { return string.Format("{0} {1}", FirstName, LastName).Trim(); } }
    public bool IsMatchResult { get; set; }

    public virtual ICollection<Title> Titles { get; set; }
    public virtual ICollection<MatchParticipant> MatchesParticipated { get; set; }
    public virtual ICollection<MatchResult> MatchResults { get; set; }
    public virtual ICollection<TagTeam> TagTeams { get; set; }

    public virtual ICollection<Brand> Brands { get; set; }

}

public class Brand
{
    [Key]
    public Guid ID { get; set; }
    public string Name { get; set; }
    public Guid? ParentID { get; set; }

    [ForeignKey("ParentID")]
    public virtual Brand Parent { get; set; }
    public virtual ICollection<Event> Events { get; set; }

    public virtual ICollection<Competitor> Roster { get; set; }

}

modelBuilder.Entity<Brand>()
            .HasMany<Competitor>(c => c.Roster)
            .WithMany()
            .Map(mp =>
            {
                mp.MapLeftKey("BrandID");
                mp.MapRightKey("CompetitorID");
                mp.ToTable("BrandCompetitors");
            });
我不明白为什么它要在Brand上创建新的外键列,而不仅仅是链接表。

问题在于.WithMany。您有显式导航属性,但未在.WithMany中指定它

因此,请按如下方式编写配置:

modelBuilder.Entity<Brand>()
            .HasMany<Competitor>(b => b.Roster)
            .WithMany(c => c.Brands) // <-- Here it is
            .Map(mp =>
            {
                mp.MapLeftKey("BrandID");
                mp.MapRightKey("CompetitorID");
                mp.ToTable("BrandCompetitors");
            });

现在它将按预期生成所有内容

.Net核心还是框架?它是.Net框架是的,这解决了它。非常感谢。
modelBuilder.Entity<Brand>()
            .HasMany<Competitor>(b => b.Roster)
            .WithMany(c => c.Brands) // <-- Here it is
            .Map(mp =>
            {
                mp.MapLeftKey("BrandID");
                mp.MapRightKey("CompetitorID");
                mp.ToTable("BrandCompetitors");
            });