我的问题出在带有Fluent API的EF Core 2.2中。我需要在一个表中生成 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])
);
我弄清楚问题出在哪里,我在Employee
中添加了 2 个ICollection<RequestTracking>
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; }
}