首先在 EF6 代码中创建多对多关系

Creating a many-to-many relationship in EF6 code first

我尝试首先使用代码在两个表之间创建多对多关系。

我有如下内容。

public class Railroad
{

    public int Id { get; set; }

    // Other members...

    public ICollection<StorageLocation> StorageLocations { get; set; }

}

public class StorageLocation
{

    public int Id { get; set; }

    public Provider Provider { get; set; }

    // Other members

    public ICollection<Railroad> Railroads { get; set; }

}

我读 an article 描述这是正确的方法。但是当我尝试构建迁移时,出现错误。

Unable to determine the relationship represented by navigation property 'Railroad.StorageLocations' of type 'ICollection'. Either manually configure the relationship, or ignore this property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.

你确定你没有在 EF Core 上吗? 这在 EF Core 之前已受支持,但现在不支持:

this

In previous versions of Entity Framework, this model definition was sufficient for EF to imply the correct type of relationship and to generate the join table for it. In EF Core it is necessary to include an entity in the model to represent the join table, and then add navigation properties to either side of the many-to-many relations that point to the join entity instead: