При начальной миграции создается дополнительный FK ***EntityId
Столкнулся с такой проблемой при начальной миграции создается FK EmployeeEntityId, в чем может быть причина?
Модели:
public class EmployeeEntity
{
public int Id { get; set; }
public DateOnly HiringDate { get; set; }
public DateOnly? DateOfDismissal { get; set; }
public string? Description { get; set; }
public Guid UserId { get; set; }
public UserEntity? User { get; set; }
public ICollection<DepartmentEntity> Department { get; set; } = [];
public ICollection<ReceptionEntity> Reception { get; set; } = [];
}
public class DepartmentEntity
{
public int Id { get; set; }
public string Name { get; set; } = string.Empty;
public string? Description { get; set; }
public int AdressId { get; set; }
public AdressEntity? Adress { get; set; }
public ICollection<EmployeeEntity> Employee { get; set; } = [];
public ICollection<ReceptionEntity> Reception { get; set; } = [];
}
public class EmployeeDepartmentEntity
{
public int EmployeeId { get; set; }
public int DepartmentId { get; set; }
public string? Description { get; set; }
public int PositionId { get; set; }
public PositionEntity? Position { get; set; }
}
Конфигурации
public void Configure(EntityTypeBuilder<EmployeeEntity> builder)
{
builder.HasKey(x => x.Id);
builder
.Property(x => x.HiringDate)
.IsRequired();
builder
.Property(x => x.Description)
.HasMaxLength(250);
builder
.HasMany(x => x.Reception)
.WithOne(x => x.Employee);
builder
.HasOne(e => e.User)
.WithOne(e => e.Employee)
.HasForeignKey<EmployeeEntity>(e => e.UserId)
.IsRequired();
builder.HasMany(r => r.Department)
.WithMany(p => p.Employee)
.UsingEntity<EmployeeDepartmentEntity>(
r => r.HasOne<DepartmentEntity>().WithMany().HasForeignKey(e => e.DepartmentId),
l => l.HasOne<EmployeeEntity>().WithMany().HasForeignKey(e => e.EmployeeId));
}
public void Configure(EntityTypeBuilder<DepartmentEntity> builder)
{
builder.HasKey(x => x.Id);
builder
.Property(x => x.Name)
.HasMaxLength(60)
.IsRequired();
builder
.Property(x => x.Description)
.HasMaxLength(250);
builder
.HasOne(x => x.Adress)
.WithOne(y => y.Department)
.HasForeignKey<DepartmentEntity>(x => x.AdressId);
builder.HasMany(r => r.Employee)
.WithMany(p => p.Department)
.UsingEntity<EmployeeDepartmentEntity>(
l => l.HasOne<EmployeeEntity>().WithMany().HasForeignKey(e => e.EmployeeId),
r => r.HasOne<DepartmentEntity>().WithMany().HasForeignKey(e => e.DepartmentId));
builder
.HasMany(x => x.Reception)
.WithOne(x => x.Department);
}
public void Configure(EntityTypeBuilder<EmployeeDepartmentEntity> builder)
{
builder.HasKey(r => new { r.EmployeeId, r.DepartmentId });
builder
.Property(x => x.Description)
.HasMaxLength(250);
builder
.HasOne(x => x.Position)
.WithMany(x => x.EmployeeDepartment)
.HasForeignKey(x => x.PositionId);
}
Генерируемая миграция
migrationBuilder.CreateTable(
name: "EmployeesDepartments",
columns: table => new
{
EmployeeId = table.Column<int>(type: "int", nullable: false),
DepartmentId = table.Column<int>(type: "int", nullable: false),
Description = table.Column<string>(type: "nvarchar(250)", maxLength: 250, nullable: true),
PositionId = table.Column<int>(type: "int", nullable: false),
EmployeeEntityId = table.Column<int>(type: "int", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_EmployeesDepartments", x => new { x.EmployeeId, x.DepartmentId });
table.ForeignKey(
name: "FK_EmployeesDepartments_Departments_DepartmentId",
column: x => x.DepartmentId,
principalTable: "Departments",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_EmployeesDepartments_Employees_EmployeeEntityId",
column: x => x.EmployeeEntityId,
principalTable: "Employees",
principalColumn: "Id");
table.ForeignKey(
name: "FK_EmployeesDepartments_Employees_EmployeeId",
column: x => x.EmployeeId,
principalTable: "Employees",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_EmployeesDepartments_Positions_PositionId",
column: x => x.PositionId,
principalTable: "Positions",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});