Entity framework 为什么不是';EF迁移是否生成正确的多对多关系?

Entity framework 为什么不是';EF迁移是否生成正确的多对多关系?,entity-framework,entity-framework-migrations,Entity Framework,Entity Framework Migrations,我有一些EF-Code-First表(如果它们是ApplicationUser,则有一个),我正在尝试生成一个迁移来添加多对多关系 public class ApplicationUser : IdentityUser { // many properties, not relevant here // NEW RELATIONSHIP public virtual ICollection<Trip> Trips { get; set; } = new Ha

我有一些EF-Code-First表(如果它们是ApplicationUser,则有一个),我正在尝试生成一个迁移来添加多对多关系

public class ApplicationUser : IdentityUser
{
    // many properties, not relevant here

    // NEW RELATIONSHIP
    public virtual ICollection<Trip> Trips { get; set; } = new HashSet<Trip>();
}

public class Trip
{
    // again, many properties

    public string ClientId { get; set; }

    [ForeignKey(nameof(ClientId))]
    public virtual ApplicationUser Client { get; set; }

    public string PartnerId { get; set; }

    [ForeignKey(nameof(PartnerId))]
    public virtual ApplicationUser Partner { get; set; }

    // NEW RELATIONSHIP
    public virtual ICollection<ApplicationUser> OtherUsers { get; set; } = new HashSet<ApplicationUser>();

}
为什么它要创建两个一对多关系而不是一对多关系?我希望生成第三个表

我以前做过这件事,没有任何问题,但我不知道我已经有外键这一事实是否影响了生成


我见过使用fluentapi解决这个问题,但我以前从未使用过它。这真的是解决问题的唯一办法吗?我做错了什么?

我想你需要一个新系列的
反向属性。尝试按如下方式更改您的
行程
课程

// NEW RELATIONSHIP
[InverseProperty("Trips")]
public virtual ICollection<ApplicationUser> OtherUsers { get; set; } = new HashSet<ApplicationUser>();
//新关系
[反向财产(“Trips”)]
公共虚拟ICollection

更新: 我注意到在另一个问题上的评论对于它是否有效是模棱两可的。我个人并没有在多对多的关系中尝试过,但这种方法适用于以下情况:


我很想知道它是否适用于您的情况。

不过,我仍然很好奇为什么它不会自动检测到它。由于它超出了惯例,我希望他们不想猜测。类之间的多重关系可能会使事情变得混乱。很高兴它成功了!
// NEW RELATIONSHIP
[InverseProperty("Trips")]
public virtual ICollection<ApplicationUser> OtherUsers { get; set; } = new HashSet<ApplicationUser>();