2017-10-18 109 views
0

我想包括外键关键细节作为我的查询的一部分。如何获得EF.core使用左连接,而不是内部连接

我怎样才能得到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] 

回答

1

找到了解决办法,使所有的外键为空的,然后EF.core使用了LEFT 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; } 
    } 
相关问题