2017-10-18 13 views
0

私のクエリの一部として外部キーの詳細を含めることを試みています。EF.coreでINNER JOINの代わりにLEFT JOINを使用する方法

EF.coreは、INNER JOINsの代わりにLEFT JOINsを使用するにはどうすればよいですか?

コントローラGetListので
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; } 
    } 

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; } 
    } 
関連する問題