【问题标题】:Issue with Many-To-Many relationships in MySqlMySql中的多对多关系问题
【发布时间】:2017-03-10 17:38:36
【问题描述】:

我正在使用SapientGuardian's EFCore library 将 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 add 人员时;我明白了……

        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?谁能告诉我这是怎么回事?

【问题讨论】:

  • 那是 EF6 的,它不适用于 EF Core
  • 这很奇怪,因为我只是在我的项目中使用它。
  • 那么PersonnelRole之间的关联呢? DegreePersonnelDegree 与此无关。

标签: mysql asp.net-core entity-framework-core .net-core


【解决方案1】:

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

所以我假设您配置了错误。你的情况应该是这样的:

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

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

这就是您在 OnModelCreating 方法中应该拥有的全部内容。

【讨论】:

    猜你喜欢
    • 2020-04-04
    • 1970-01-01
    • 2021-07-18
    • 1970-01-01
    • 2011-01-05
    • 1970-01-01
    • 2020-04-10
    • 2021-09-14
    相关资源
    最近更新 更多