1

我正在尝试将外键详细信息作为查询的一部分。

如何让EF.core使用LEFT JOINs而不是INNER JOINs

public class Offence
  {
    [Key]
    public Int32 offence_id { get; set; }

    public Int32 guard_id { get; set; }
    public Int32 penalty_id { get; set; }
    public DateTime? dt_recorded { get; set; }
    public Int32 salary_id { get; set; }
    public Decimal? amount { get; set; }
    public String status { get; set; }
    public Int32 site_id { get; set; }

    public Guard Guard { get; set; }
    public Salary Salary { get; set; }
    public Site Site { get; set; }
    public Penalty Penalty { get; set; }

    public DateTime? last_modified { get; set; }
    public int? last_modified_by { get; set; }
  }

在控制器 GetList

var offences = db.Offences
        .Include(e => e.Guard)
        .Include(e => e.Penalty)
        .Include(e => e.Site)
        .Include(e => e.Salary)
        .AsNoTracking();

生成的 SQL:

SELECT [e].[offence_id], [e].[amount], [e].[dt_recorded], [e].[guard_id], [e].[last_modified], [e].[last_modified_by], [e].[penalty_id], [e].[salary_id], [e].[site_id], [e].[status], [e.Salary].[salary_id], [e.Salary].[dt_paid], [e.Salary].[guard_id], [e.Salary].[last_modified], [e.Salary].[last_modified_by], [e.Salary].[period], [e.Site].[site_id], [e.Site].[address], [e.Site].[client_id], [e.Site].[last_modified], [e.Site].[last_modified_by], [e.Site].[name], [e.Site].[state], [e.Penalty].[penalty_id], [e.Penalty].[amount], [e.Penalty].[description], [e.Penalty].[dt], [e.Penalty].[last_modified], [e.Penalty].[last_modified_by], [e.Penalty].[name], [e.Guard].[guard_id], [e.Guard].[address], [e.Guard].[bank], [e.Guard].[dob], [e.Guard].[dt_joined], [e.Guard].[dt_trained], [e.Guard].[has_picture], [e.Guard].[height], [e.Guard].[last_modified], [e.Guard].[last_modified_by], [e.Guard].[location_id], [e.Guard].[marital_status], [e.Guard].[mobiles], [e.Guard].[name], [e.Guard].[nuban], [e.Guard].[ref_no], [e.Guard].[religion], [e.Guard].[salary], [e.Guard].[sex], [e.Guard].[state_origin], [e.Guard].[status]
FROM [Offences] AS [e]
left JOIN [Salaries] AS [e.Salary] ON [e].[salary_id] = [e.Salary].[salary_id]
left JOIN [Sites] AS [e.Site] ON [e].[site_id] = [e.Site].[site_id]
left JOIN [Penalties] AS [e.Penalty] ON [e].[penalty_id] = [e.Penalty].[penalty_id]
left JOIN [Guards] AS [e.Guard] ON [e].[guard_id] = [e.Guard].[guard_id]
ORDER BY [e.Guard].[name]
4

1 回答 1

5

找到解决方案,使所有外键都可以为空,然后EF.core使用 aLEFT JOIN代替查询:

  public class Offence
  {
    [Key]
    public Int32 offence_id { get; set; }

    public Int32? guard_id { get; set; } // make null-able
    public Int32? penalty_id { get; set; } // make null-able
    public DateTime? dt_recorded { get; set; }
    public Int32? salary_id { get; set; } // make null-able
    public Decimal? amount { get; set; }
    public String status { get; set; }
    public Int32? site_id { get; set; } // make null-able

    public Guard Guard { get; set; }
    public Salary Salary { get; set; }
    public Site Site { get; set; }
    public Penalty Penalty { get; set; }

    public DateTime? last_modified { get; set; }
    public int? last_modified_by { get; set; }
  }
于 2017-10-18T19:21:07.967 回答