efcore icon indicating copy to clipboard operation
efcore copied to clipboard

How to Use Navigation Property generate left join sql not inner join sql

Open ByZhouhang opened this issue 5 years ago • 9 comments

When Use Navigate Property one to one:

Normally the sql is a inner join , I do not know how to write a left Join use one to one Navigate Property

usually,we can also use Include to make ef generate a left join sql ,there is other method?

I donot like linq like this:

var LeftJoin = from a in aList
               join b in bList on a.Id equals b.Id into cc
               from b in cc.DefaultIfEmpty()

can implent like this?

      _context.Doctor.where(t=>t.Id==id)
                    .Select(doctor => new DoctorDTO()
                     {
                         Name=doctor.Name,
                        HospitalName=doctor.Hospital.Name  // want  left join  not innner join
                      }

In project ,i use automapper ProjectTo< DTO >,but how can i use One to One Navigation property generate left join sql?

ByZhouhang avatar Nov 26 '21 01:11 ByZhouhang

Have you tried include ?

kdehia avatar Nov 26 '21 04:11 kdehia

Have you tried include ? yes ,normally we can use include. but when use automapper ProjectTo < DTO > , Automapper seems to ignore include,always generate sql inner join , maybe i should ask automapper . thank you ~

ByZhouhang avatar Nov 26 '21 04:11 ByZhouhang

@ByZhouhang EF generates the join that is appropriate for your model definition. Please attach a small, runnable project or post a small, runnable code listing that generates an inner join when you think it should be a left join so that we can investigate.

ajcvickers avatar Nov 26 '21 10:11 ajcvickers

@ByZhouhang EF generates the join that is appropriate for your model definition. Please attach a small, runnable project or post a small, runnable code listing that generates an inner join when you think it should be a left join so that we can investigate.

thanks, i found one to one relation , the sql ef generate left join or inner join will depend on config

public class   Doctor
{
    public Guid  Id {get ;set;}
    public string FirstName{ get; set; }
    // public Guid  HospitalId {get ;set;} //this will result inner join 
    public Guid?  HospitalId {get ;set;} //this will result left join 

    public Hospital Hospital { get; set; }    // navigate property
}

public class   Hospital 
{
    public string Name { get; set; }      
}

ByZhouhang avatar Dec 01 '21 02:12 ByZhouhang

@ByZhouhang EF generates the join that is appropriate for your model definition. Please attach a small, runnable project or post a small, runnable code listing that generates an inner join when you think it should be a left join so that we can investigate.

public class   UserAnswer
{
    public Guid  Id {get ;set;}     
    public string  Answer { get; set; }   
    public Guid  UserId   {get ;set;}     

    public Guid  TrialQuestionId {get ;set;}     
    public TrialQuestion  TrialQuestion{ get; set; }    // navigate property
}

public class   TrialQuestion
{
    public Guid  Id {get ;set;}
    public string QuestionName { get; set; }      
}

how to config make ef know right join ?

_context.UserAnswer
    .Where(t=>t.UserId   ==id)
    .Select(answer=> new AnswerDTO()
    {
        Answer =answer.Answer ,
        QuestionName =answer.TrialQuestion.QuestionName   // want right join   becasue  maybe user not write answer
    }

ByZhouhang avatar Dec 01 '21 03:12 ByZhouhang

@ByZhouhang as with your first code sample (Doctor and Hospital), if you make UserAnswer.TrialQuestionId nullable, EF Core will produce a LEFT JOIN, since a UserAnswer might not have a TrialQuestion; the "requiredness" of navigations is determined by the nullability of their foreign keys, and that's what determines whether an INNER or LEFT join is used.

EF Core doesn't generate RIGHT JOIN, but these are logically the same as LEFT JOIN.

roji avatar Dec 01 '21 07:12 roji

@roji I want to start search with UserAnswer ,so it must be right join . if user not answer ,there will no db answer record, i want to present the question if user not answer.

if i start search with TrialQuestion left join UserAnswer will meet requirements,i just want to know is there any other method can meet requirements start search with UserAnswer

ByZhouhang avatar Dec 01 '21 07:12 ByZhouhang

@ByZhouhang AFAIK EF Core does not generate RIGHT JOIN - it would be a bit difficult to imagine the LINQ that would be used to express such a query; you'll have to start your LINQ query from UserAnswer. Note that RIGHT JOINs are mathematically rewritable as LEFT JOINs. Do you have any particular reason to want to start with UserAnswer rather than TrialQuestion, or is a matter of personal preference?

roji avatar Dec 01 '21 08:12 roji

@roji deeply apreciate ,I was just curious ~

ByZhouhang avatar Dec 01 '21 08:12 ByZhouhang