一个表中有2个相同的外键。流利的Api生成3个外键而不是2个

时间:2019-03-28 00:53:53

标签: c# entity-framework-core ef-fluent-api

我的问题是使用Fluent API的EF Core 2.2。我需要在一个表中而不是2个表中生成3个外键,它会生成我的表不需要的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 个答案:

答案 0 :(得分:0)

我找出了问题所在,我在ICollection<RequestTracking>中添加了2个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; }
}