#c# #entity-framework-core #ef-fluent-api
#c# #entity-framework-core #ef-fluent-api
Вопрос:
Моя проблема в EF Core 2.2 с Fluent API. Мне нужно сгенерировать 3 внешних ключа в одной таблице вместо 2, он генерирует EmployeeId
то, что не требуется для моей таблицы.
Вот мой код:
builder.ToTable("RequestTracking", "Communication");
builder.Property(r => r.Id)
.ValueGeneratedOnAdd();
builder.HasOne(r => r.Request)
.WithMany(r => r.RequestTrackings);
builder.HasOne(r => r.Employee)
.WithMany()
.HasForeignKey(r => r.FromEmployeeId)
.OnDelete(DeleteBehavior.Restrict);
builder.HasOne(r => r.Employee)
.WithMany()
.HasForeignKey(r => r.ToEmployeeId)
.OnDelete(DeleteBehavior.Restrict);
public class RequestTracking
{
public int Id { get; set; }
public int RequestId { get; set; }
public Request Request { get; set; }
public int FromEmployeeId { get; set; }
public int ToEmployeeId { get; set; }
public Employee Employee { get; set; }
}
public class Employee
{
public int Id { get; set; }
public string FirstName { get; set; }
public string MiddleName { get; set; }
public string LastName { get; set; }
public DateTime Birthdate { get; set; }
public Gender Gender { get; set; }
public AppUser User { get; set; }
public ICollection<EmployeeStatus> EmployeesStatus { get; set; }
public ICollection<Resignation> Resignations { get; set; }
public ICollection<RequestTracking> RequestTrackings { get; set; }
}
Я также пробовал это:
builder.ToTable("RequestTracking", "Communication");
builder.Property(r => r.Id)
.ValueGeneratedOnAdd();
builder.HasOne(r => r.Request)
.WithMany(r => r.RequestTrackings);
builder.HasOne(r => r.FromEmployee)
.WithMany()
.HasForeignKey(r => r.FromEmployeeId)
.OnDelete(DeleteBehavior.Restrict);
builder.HasOne(r => r.ToEmployee)
.WithMany()
.HasForeignKey(r => r.ToEmployeeId)
.OnDelete(DeleteBehavior.Restrict);
public class RequestTracking
{
public int Id { get; set; }
public int RequestId { get; set; }
public Request Request { get; set; }
public int FromEmployeeId { get; set; }
public Employee FromEmployee { get; set; }
public int ToEmployeeId { get; set; }
public Employee ToEmployee { get; set; }
}
Вот таблица, сгенерированная EF Core
CREATE TABLE [Communication].[RequestTracking]
(
[Id] INT IDENTITY(1, 1) NOT NULL,
[RequestId] INT NOT NULL,
[FromEmployeeId] INT NOT NULL,
[ToEmployeeId] INT NOT NULL,
[EmployeeId] INT NULL, // Here is my problem it generates this column
CONSTRAINT [PK_RequestTracking]
PRIMARY KEY CLUSTERED ([Id] ASC),
CONSTRAINT [FK_RequestTracking_Employee_EmployeeId]
FOREIGN KEY ([EmployeeId]) REFERENCES [HR].[Employee] ([Id]),
CONSTRAINT [FK_RequestTracking_Request_RequestId]
FOREIGN KEY ([RequestId]) REFERENCES [Communication].[Request] ([Id]) ON DELETE CASCADE,
CONSTRAINT [FK_RequestTracking_Employee_ToEmployeeId]
FOREIGN KEY ([ToEmployeeId]) REFERENCES [HR].[Employee] ([Id])
);
Ответ №1:
Я выясняю, в чем проблема, я добавляю 2 ICollection<RequestTracking>
в Employee
public class Employee
{
public int Id { get; set; }
public string FirstName { get; set; }
public string MiddleName { get; set; }
public string LastName { get; set; }
public DateTime Birthdate { get; set; }
public Gender Gender { get; set; }
public AppUser User { get; set; }
public ICollection<EmployeeStatus> EmployeesStatus { get; set; }
public ICollection<Resignation> Resignations { get; set; }
public ICollection<RequestTracking> RequestTrackingsFrom { get; set; }
public ICollection<RequestTracking> RequestTrackingsTo { get; set; }
}