.NET Core 5.0 EF Migration 添加新的外列

.NET Core 5.0 EF Migration add new foreign column

我有一个带有 .net 核心 Entity Framework 代码的应用程序。

我在关系中有 2 个表。 altKategori 和 anaKategori

alt类别

public class altKategori
{
    [Key]
    public int idAltKategori { get; set; }

    [Column(TypeName = "Varchar")]
    [StringLength(30)]
    public string adAltKategori { get; set; }

    public int idAnaKategori { get; set; }
    public anaKategori anaKategori { get; set; }
}

分类

public class anaKategori
{
    [Key]
    public int idAnaKategori { get; set; }

    [Column(TypeName = "Varchar")]
    [StringLength(30)]
    public string adAnaKategori { get; set; }

    public List<altKategori> altKategoris { get; set; }
}

还有我的上下文:

public class Context : DbContext
{
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer("server=.\MSSQLSERVER2019;database=deneme;User ID=deneme1;Password=****;");
    }
    public DbSet<altKategori> altKategoris { get; set; }
    public DbSet<anaKategori> anaKategoris { get; set; }
}

当我开始迁移时,迁移会自动添加 anaKategoriidAnaKategori 列,并添加与该列的关系。

migrationBuilder.CreateTable(
            name: "altKategoris",
            columns: table => new
            {
                idAltKategori = table.Column<int>(type: "int", nullable: false)
                    .Annotation("SqlServer:Identity", "1, 1"),
                adAltKategori = table.Column<string>(type: "Varchar(30)", maxLength: 30, nullable: true),
                idAnaKategori = table.Column<int>(type: "int", nullable: false),
                anaKategoriidAnaKategori = table.Column<int>(type: "int", nullable: true)
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_altKategoris", x => x.idAltKategori);
                table.ForeignKey(
                    name: "FK_altKategoris_anaKategoris_anaKategoriidAnaKategori",
                    column: x => x.anaKategoriidAnaKategori,
                    principalTable: "anaKategoris",
                    principalColumn: "idAnaKategori",
                    onDelete: ReferentialAction.Restrict);
            });

我不想与 anaKategoriidAnaKategori 建立关系。我想与 idAnaKategori 建立联系。我怎么能够? 感谢您的帮助。

尝试添加关系属性

public class altKategori
{
    [Key]
    public int idAltKategori { get; set; }

    [Column(TypeName = "Varchar")]
    [StringLength(30)]
    public string adAltKategori { get; set; }

    public int idAnaKategori { get; set; }

    [ForeignKey(nameof(idAnaKategori ))]
    [InverseProperty("altKategoris")]
    public anaKategori anaKategori { get; set; }
}

public class anaKategori
{
    [Key]
    public int idAnaKategori { get; set; }

    [Column(TypeName = "Varchar")]
    [StringLength(30)]
    public string adAnaKategori { get; set; }

   [InverseProperty(nameof(altKategori.anaKategori))]
    public List<altKategori> altKategoris { get; set; }
}