此问题已在此处有答案:
How can I add an Entity Framework Core n-m self-referencing entity using an explicit join table with payload for SQL-Server?(1个答案)
Many-to-many self referencing relationship(1个答案)
6天前关闭。
我有一个Station实体(在公交车站的上下文中)。此站点实体具有表示目的地的站点集合。如何在EF Core中将其Map为多对多关系?
public class Station {
[Key]
public int StationId { get; set; }
[Required]
[Column(TypeName = "VARCHAR")]
[StringLength(100)]
public string TerminalName { get; set; }
[Required]
[Column(TypeName = "VARCHAR")]
[StringLength(100)]
public string City { get; set; }
[Required]
[Column(TypeName = "VARCHAR")]
[StringLength(100)]
public string Province { get; set; }
public ICollection<Station> Destinations { get; set; }
}
我在做这个的时候遇到了麻烦,在为此运行迁移时,它产生了这个错误:The navigation 'Station.Destinations' cannot be used for both sides of a many-to-many relationship. Many-to-many relationships must use two distinct navigation properties.
2条答案
按热度按时间qf9go6mv1#
我们无法以一流的方式对无向图进行建模。链接表必须有一个FromStation,ToStation,如果你总是可以双向,你需要两个条目。所以试着
5q4ezhmt2#
首先你需要这个额外的类
接下来在你的电台类中添加两个列表
然后创建一个Map文件夹,并在该文件夹中添加类,并使用
IEntityTypeConfiguration
最后配置
Db