MySql中的多對多關係問題

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

我使用SapientGuardian的EFCore庫使用EF Core和MySQL,我正在嘗試使用以下代碼創建一個具有多對多關係的表;

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

現在,當我運行dotnet ef migration添加人員時;我明白了......

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

請注意,表定義中的Role具有PersonnelId列。 Personnel表有RoleId嗎?誰能告訴我這裡發生了什麼?

熱門答案

我有相同的模型,但不同的OnModelCreating方法。

所以我假設你配置錯了。對於你的情況應該是這樣的:

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

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

這就是你在OnModelCreating方法中應該擁有的一切。



Related

許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因