I have the following entity:
public class Level
{
public int LevelId { get; set; }
public int? ParentLevelId { get; set; }
public string Name { get; set; }
public virtual Level Parent { get; set; }
public virtual HashSet<Level> Children { get; set; }
}
What I am having trouble here, is the Children
property, which is configured like this in Fluent API:
modelBuilder.Entity<Level>()
.HasOne(x => x.Parent)
.WithMany(x => x.Children)
.HasForeignKey(x => x.ParentLevelId);
This results in some additional column being added by the migration:
migrationBuilder.AddColumn<int>(
name: "LevelId1",
table: "Level",
nullable: true);
migrationBuilder.CreateIndex(
name: "IX_Level_LevelId1",
table: "Level",
column: "LevelId1");
migrationBuilder.AddForeignKey(
name: "FK_Level_Level_LevelId1",
table: "Level",
column: "LevelId1",
principalTable: "Level",
principalColumn: "LevelId",
onDelete: ReferentialAction.Restrict);
What am I doing wrong here?
Edit: Question was marked as a possible duplicate of this question; however, in that case, the model generation works - the issue is loading the data. Whereas here, the issue is that an additional column is generated.