简体   繁体   中英

Issue with Many-To-Many relationships in MySql

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM