I have an application with .net core Entity Framework code first.
I have 2 tables in relationships. altKategori and anaKategori
altKategoris
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; }
}
anaKategoris
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; }
}
Also there is my Context:
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; }
}
When I start migration, migration automatic add anaKategoriidAnaKategori columns, and add relationship with that column.
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);
});
I don't want to relation with anaKategoriidAnaKategori. I want to relation with idAnaKategori. How can I? Thanks for your help.
CodePudding user response:
try to add relation attributes
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; }
}