0

当我尝试使用.Include()扩展方法使用 Entity Framework Core 3.1.3 时,我在诊断错误时遇到了一些问题(无效的列名 'AgencyOfficeId' )。我正在尝试返回父记录(AgencyEmployee)和相应关系记录的集合。下面是我尝试运行以返回 List 的 C# 代码,其中包括其关联的 AgencyOfficeAgencyEmployeeRelationship 记录。(C# 模型和 DbContext 配置如下。)

return _context.AgencyEmployee
   .Include(xref => xref.AgencyOfficeAgencyEmployeeRelationships)
   .ToList();

EF Core 在运行上述代码时生成的原始 SQL 如下:

SELECT [a].[Id], 
   [a].[AgencyOfficeId],    <----- EF Core is adding this column for some reason
   [a].[DisplayName], 
   [a].[FirstName], 
   [a].[LastName], 
   [a].[MiddleName], 
   [a].[PreferredName], 
   [a].[Suffix], 
   [a].[Title],
   [a0].[Id], 
   [a0].[AgencyEmployeeId],
   [a0].[AgencyOfficeId],
   [a0].[IsPrimaryInRole], 
   [a0].[JobTitle]
FROM [entity].[AgencyEmployees] AS [a]
   LEFT JOIN [entity].[AgencyOfficeAgencyEmployeeRelationships] AS [a0] ON [a].[Id] = [a0]. 
[AgencyEmployeeId]
ORDER BY [a].[Id], [a0].[Id]

我有三个模型代表 MS SQL Server 中的表:AgencyOfficeAgencyEmployeeAgencyOfficeAgencyEmployeeRelationship。我正在使用 EFPowerTools 的逆向工程师功能生成以下 C# 模型:

机构员工

[Table("AgencyEmployees", Schema = "entity")]
public partial class AgencyEmployee
{
    public AgencyEmployee()
    {
        AgencyOfficeAgencyEmployeeRelationships = new HashSet<AgencyOfficeAgencyEmployeeRelationship>();
    }

    [StringLength(50)]
    public string Title { get; set; }
    [StringLength(200)]
    public string FirstName { get; set; }
    [StringLength(200)]
    public string MiddleName { get; set; }
    [Required]
    [StringLength(200)]
    public string LastName { get; set; }
    [StringLength(50)]
    public string Suffix { get; set; }
    [StringLength(100)]
    public string PreferredName { get; set; }
    [Required]
    [StringLength(500)]
    public string DisplayName { get; set; }
    [Key]
    public Guid Id { get; set; }

    [InverseProperty(nameof(AgencyOfficeAgencyEmployeeRelationship.AgencyEmployee))]
    public virtual ICollection<AgencyOfficeAgencyEmployeeRelationship> AgencyOfficeAgencyEmployeeRelationships { get; set; }
}

代理处

[Table("AgencyOffices", Schema = "entity")]
public partial class AgencyOffice
{
    public AgencyOffice()
    {
        AgencyOfficeAgencyEmployeeRelationships = new HashSet<AgencyOfficeAgencyEmployeeRelationship>();
    }

    [Required]
    [StringLength(250)]
    public string FormalName { get; set; }
    [Required]
    [StringLength(250)]
    public string ShortName { get; set; }
    [Required]
    [StringLength(250)]
    public string ReportName { get; set; }
    [Column("FEIN")]
    [StringLength(100)]
    public string Fein { get; set; }
    [StringLength(100)]
    public string OfficeCode { get; set; }
    [StringLength(100)]
    public string DomicileState { get; set; }
    [Column(TypeName = "date")]
    public DateTime? AppointmentDate { get; set; }
    [Column(TypeName = "date")]
    public DateTime? TerminationDate { get; set; }
    public bool IsSatelliteOffice { get; set; }
    [Key]
    public Guid Id { get; set; }

    [InverseProperty(nameof(AgencyOfficeAgencyEmployeeRelationship.AgencyOffice))]
    public virtual ICollection<AgencyOfficeAgencyEmployeeRelationship> AgencyOfficeAgencyEmployeeRelationships { get; set; }
}

AgencyOfficeAgencyEmployeeRelationship :

[Table("AgencyOfficeAgencyEmployeeRelationships", Schema = "entity")]
public partial class AgencyOfficeAgencyEmployeeRelationship
{
    public Guid AgencyOfficeId { get; set; }
    public Guid AgencyEmployeeId { get; set; }
    [StringLength(100)]
    public string JobTitle { get; set; }
    public bool IsPrimaryInRole { get; set; }
    [Key]
    public Guid Id { get; set; }

    [ForeignKey(nameof(AgencyEmployeeId))]
    [InverseProperty("AgencyOfficeAgencyEmployeeRelationships")]
    public virtual AgencyEmployee AgencyEmployee { get; set; }

    [ForeignKey(nameof(AgencyOfficeId))]
    [InverseProperty("AgencyOfficeAgencyEmployeeRelationships")]
    public virtual AgencyOffice AgencyOffice { get; set; }
}

这是OnModelCreating()方法中每个表/模型的 DbContext 配置

modelBuilder.Entity<AgencyEmployee>(entity =>
{
   entity.ToTable("AgencyEmployees", "entity");

   entity.Property(e => e.Id).HasDefaultValueSql("(newid())");

   entity.Property(e => e.DisplayName)
      .IsRequired()
      .HasMaxLength(500);

   entity.Property(e => e.FirstName).HasMaxLength(200);

   entity.Property(e => e.LastName)
      .IsRequired()
      .HasMaxLength(200);

   entity.Property(e => e.MiddleName).HasMaxLength(200);

   entity.Property(e => e.PreferredName).HasMaxLength(100);

   entity.Property(e => e.Suffix).HasMaxLength(50);

   entity.Property(e => e.Title).HasMaxLength(50);
});

modelBuilder.Entity<AgencyOffice>(entity =>
{
   entity.ToTable("AgencyOffices", "entity");

   entity.Property(e => e.Id).HasDefaultValueSql("(newid())");

   entity.Property(e => e.AppointmentDate).HasColumnType("date");

   entity.Property(e => e.DomicileState).HasMaxLength(100);

   entity.Property(e => e.Fein)
      .HasColumnName("FEIN")
      .HasMaxLength(100);

   entity.Property(e => e.FormalName)
      .IsRequired()
      .HasMaxLength(250);

   entity.Property(e => e.OfficeCode).HasMaxLength(100);

   entity.Property(e => e.ReportName)
      .IsRequired()
      .HasMaxLength(250);

   entity.Property(e => e.ShortName)
      .IsRequired()
      .HasMaxLength(250);

   entity.Property(e => e.TerminationDate).HasColumnType("date");
});

modelBuilder.Entity<AgencyOfficeAgencyEmployeeRelationship>(entity =>
{
   entity.ToTable("AgencyOfficeAgencyEmployeeRelationships", "entity");

   entity.HasIndex(e => new { e.AgencyOfficeId, e.AgencyEmployeeId })
      .HasName("UQ_entity_AgencyOfficeAgencyEmployeeRelationships")
      .IsUnique();

   entity.Property(e => e.Id).HasDefaultValueSql("(newid())");

   entity.Property(e => e.JobTitle).HasMaxLength(100);

   entity.HasOne(d => d.AgencyEmployee)
      .WithMany(p => p.AgencyOfficeAgencyEmployeeRelationships)
      .HasForeignKey(d => d.AgencyEmployeeId)
      .OnDelete(DeleteBehavior.ClientSetNull)
      .HasConstraintName("FK_AgencyOfficeAgencyEmployeeRelationships_AgencyEmployeeId");

   entity.HasOne(d => d.AgencyOffice)
      .WithMany(p => p.AgencyOfficeAgencyEmployeeRelationships)
      .HasForeignKey(d => d.AgencyOfficeId)
      .OnDelete(DeleteBehavior.ClientSetNull)
      .HasConstraintName("FK_AgencyOfficeAgencyEmployeeRelationships_AgencyOfficeId");
});

下面是 MS SQL Server 表是如何设置的关系图: SQL Server关系图

有人对我可能出错的地方有任何想法吗?如果我尝试选择AgencyOffices和相关关系记录,EF Core 会生成有效的 SQL,而不需要任何额外的字段。仅当我尝试选择AgencyEmployees时才会出现问题。

如果有人需要任何其他信息,请告诉我。感谢您抽出宝贵的时间!

4

0 回答 0