1
votes

I'm kinda stuck on a pretty simple problem.

I followed the MSDN to configure my many to many relationship but it does not work.

When I run the command to add a migration I get this error.

Unable to determine the relationship represented by navigation property 'Command.Platforms' of type 'ICollection'. Either manually configure the relationship, or ignore this property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.

Do you know how to configure a many to many relationship without having to specify explicitly the joint table ?

Thanks for your answers.

These are the classes I want to configure with entity framework core. The classes I want to perform many to many relation are Command and Platform. Don't take Parameters into account.

Command class :

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

Platform class:

    public class Platform
    {
        [Key]
        [JsonIgnore]
        public int PlatformId { get; set; }

        [MaxLength(50)]
        public string Name { get; set; }

        public ICollection<Command> Commands { get; set; }
    }

My DbContext class:

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

EDIT : Ok I just found out it will only be available in EF core 5. https://youtu.be/W1sxepfIMRM?t=751

1
Well, you could opt. for explicit, as described here, but, I would thought this would work. You could try, btw, to enable lazy loading by making the ICollections virtual. It won't solve your problem, but it might be a little more eficient.Stefan
Hello Stefan, I understand that the problem could be solved but I just wanted know if there was a way to directly access the other table without having to play with the joint table.Kamigo

1 Answers

0
votes

You have add to class Platform a property CommandId as a foreign key of Command class