简体   繁体   English

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

[英]Creating a many-to-many relationship in EF6 code first

I'm trying to use code first to create a many-to-many relationship between two tables.我试图首先使用代码在两个表之间创建多对多关系。

I have something that looks like the following.我有如下所示的东西。

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

}

I read an article that described this being the correct way to do it.我读了一篇文章,描述这是正确的方法。 But when I try to build the migration, I get an error.但是当我尝试构建迁移时,我得到一个错误。

Unable to determine the relationship represented by navigation property 'Railroad.StorageLocations' of type 'ICollection'.无法确定“ICollection”类型的导航属性“Railroad.StorageLocations”表示的关系。 Either manually configure the relationship, or ignore this property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.手动配置关系,或使用“[NotMapped]”属性或使用“OnModelCreating”中的“EntityTypeBuilder.Ignore”忽略此属性。

Are you sure you're not on EF Core ?你确定你不在EF Core上吗? this has been supported before but not now in EF Core:这在 EF Core 之前已支持但现在不支持:

See 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.在以前版本的实体框架中,这个 model 定义足以让 EF 暗示正确的关系类型并为其生成连接表。 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:在 EF Core 中,必须在 model 中包含一个实体来表示连接表,然后将导航属性添加到指向连接实体的多对多关系的任一侧:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM