Issue with Many-To-Many relationships in MySql

.net-core asp.net-core entity-framework-core mysql

Question

I am using EF Core with MySQL using SapientGuardian's EFCore library and I am trying to create a table with many-to-many relationship using the following code;

        public class Personnel
        {
            public int Id { get; set; }

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

            public virtual ICollection<PersonnelDegree> PersonnelDegrees { get; set; }
        }

        public class Degree
        {
            public int Id { get; set; }

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

            public virtual ICollection<PersonnelDegree> PersonnelDegrees { get; set; }
        }

        public class PersonnelDegree
        {
            public int PersonnelId { get; set; }

            public int DegreeId { get; set; }

            public virtual Personnel Personnel { get; set; }

            public virtual Degree Degree { get; set; }
        }

        // Inside the OnModelCreating override
        builder.Entity<Degree>().HasMany(x => x.Personnel);
        builder.Entity<Personnel>().HasMany(x => x.Degrees);

        builder.Entity<PersonnelDegree>()
            .HasKey(x => new { x.DegreeId, x.PersonnelId });

        builder.Entity<PersonnelDegree>()
            .HasOne(x => x.Degree)
            .WithMany(x => x.PersonnelDegrees)
            .HasForeignKey(x => x.DegreeId);

        builder.Entity<PersonnelDegree>()
            .HasOne(x => x.Personnel)
            .WithMany(x => x.PersonnelDegrees)
            .HasForeignKey(x => x.PersonnelId);

Now, when I run dotnet ef migration add personnel; I get this...

        migrationBuilder.CreateTable(
            name: "Role",
            columns: table => new
            {
                Id = table.Column<int>(nullable: false)
                    .Annotation("MySQL:AutoIncrement", true),
                ApplicationId = table.Column<int>(nullable: true),
                Name = table.Column<string>(maxLength: 100, nullable: true),
                PersonnelId = table.Column<int>(nullable: true) // Where is this coming from?
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_Role", x => x.Id);
                table.ForeignKey(
                    name: "FK_Role_Application_ApplicationId",
                    column: x => x.ApplicationId,
                    principalTable: "Application",
                    principalColumn: "Id",
                    onDelete: ReferentialAction.Restrict);
            });

Notice that the table definition has for Role has PersonnelId column in it. And Personnel table has RoleId? Can anybody tell me what is going on here?

1
0
12/5/2016 10:28:29 PM

Popular Answer

I have the same models, but different OnModelCreating method.

So I assume that you configured something wrong. It should be something like this for your case:

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<PersonnelDegree>()
            .HasKey(t => new { t.PersonnelId , t.DegreeId });
    }

That is all you should have in your OnModelCreating method.

0
3/24/2018 9:07:16 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow