簡體   English   中英

實體框架核心多對多無法確定關系

[英]Entity framework core many to many unable to determine relationship

我有點困在一個非常簡單的問題上。

我按照MSDN配置了我的多對多關系,但它不起作用。

當我運行命令添加遷移時,出現此錯誤。

無法確定類型為“ICollection”的導航屬性“Command.Platforms”表示的關系。 手動配置關系,或使用“[NotMapped]”屬性或使用“OnModelCreating”中的“EntityTypeBuilder.Ignore”忽略此屬性。

您知道如何配置多對多關系而無需明確指定聯合表嗎?

感謝您的回答。

這些是我想用實體框架核心配置的類。 我想要執行多對多關系的類是命令和平台。 不要考慮參數。

命令類:

    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 核心 5 中使用。 https://youtu.be/W1sxepfIMRM?t=751

您已將屬性 CommandId 添加到類 Platform 中作為 Command 類的外鍵

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM