2 个相同的外键合二为一 table。 Fluent Api 生成 3 个外键而不是 2 个
2 same foreign keys in one table. Fluent Api generate 3 foreign key instead of 2
我的问题出在使用 Fluent API 的 EF Core 2.2 中。我需要在一个 table 中生成 3 个外键而不是 2 个,它生成 EmployeeId
而我的 table 不需要。
这是我的代码:
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
生成的 Table
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; }
}
我的问题出在使用 Fluent API 的 EF Core 2.2 中。我需要在一个 table 中生成 3 个外键而不是 2 个,它生成 EmployeeId
而我的 table 不需要。
这是我的代码:
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
生成的 TableCREATE 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
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; }
}