How to Use Navigation Property generate left join sql not inner join sql
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?
Have you tried include ?
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 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.
@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 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 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 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 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 deeply apreciate ,I was just curious ~