如何解决实体框架核心多对多无法确定关系
我有点陷入一个非常简单的问题。
我遵循MSDN来配置我的多对多关系,但是它不起作用。
当我运行添加迁移的命令时,出现此错误。
无法确定类型为“ ICollection”的导航属性“ Command.Platforms”表示的关系。要么 手动配置关系,或使用 '[NotMapped]'属性或通过使用'EntityTypeBuilder.Ignore' “ OnModelCreating”。
您知道如何配置多对多关系而不必显式指定联合表吗?
感谢您的回答。
这些是我要使用实体框架核心配置的类。 我想执行多对多关系的类是Command和Platform。 不要考虑参数。
命令类:
public class Command
{
[Key]
[JsonIgnore]
public int CommandId { get; set; }
[Required]
[MaxLength(30)]
public string Name { get; set; }
[Required]
[MaxLength(250)]
public string Description { get; set; }
[MaxLength(250)]
public string Example { get; set; }
public ICollection<Platform> Platforms { get; set; }
public List<Parameter> Parameters { get; set; }
}
平台类:
public class Platform
{
[Key]
[JsonIgnore]
public int PlatformId { get; set; }
[MaxLength(50)]
public string Name { get; set; }
public ICollection<Command> Commands { get; set; }
}
我的 DbContext 类:
public class CommandsDbContext : DbContext
{
public CommandsDbContext(DbContextOptions<CommandsDbContext> options) : base(options)
{
}
public DbSet<Command> Commands { get; set; }
public DbSet<Parameter> CommandParameters { get; set; }
public DbSet<Platform> Platforms { get; set; }
}
编辑: 好的,我发现它仅在EF core 5中可用。 https://youtu.be/W1sxepfIMRM?t=751
解决方法
您已经在Platform类中添加了CommandId属性作为Command类的外键
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。