首页 > 解决方案 > 首先在 EF6 代码中创建多对多关系

问题描述

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

我有如下所示的东西。

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; }

}

我读了一篇文章,描述这是正确的方法。但是当我尝试构建迁移时,我得到一个错误。

无法确定“ICollection”类型的导航属性“Railroad.StorageLocations”表示的关系。手动配置关系,或使用“[NotMapped]”属性或使用“OnModelCreating”中的“EntityTypeBuilder.Ignore”忽略此属性。

标签: c#.net-coreentity-framework-6ef-code-first

解决方案


你确定你不上EF Core?这在 EF Core 之前已支持但现在不支持:

看到这个

在实体框架的早期版本中,此模型定义足以让 EF 暗示正确的关系类型并为其生成连接表。在 EF Core 中,必须在模型中包含一个实体来表示连接表,然后将导航属性添加到指向连接实体的多对多关系的任一侧:


推荐阅读