Answered by:
I am Use GroupJoin in Linq by EF but generate inner join

Question
-
Hi
I write this Linq Query to generate left outer join :
DbContext.GetQuery<CarSalesBN>()
.Where(bimeName => bimeName.Id == 8944731)
.GroupJoin(DbContext.GetQuery<CarSalesElh>()
, bn => new { BID = bn.Id, ELHNO = bn.ElhNo }, elhaghie => new { BID = elhaghie.BId, ELHNO = elhaghie.ElhNo }
, (bn, elhaghie) => new
{
bn.Id,
bn.ElhNo,
bn.BeginDate,
Elhaghie = elhaghie.DefaultIfEmpty()
})
.Select(bimeName => new { Id = bimeName.Id, ElhNo = bimeName.ElhNo, BeginDate = bimeName.BeginDate, Elhaghie = bimeName.Elhaghie });but, result of this query has been inner join. I investigate this problem by detail, so see that because i have Asociation for two entities CarSalesBN,CarSalesElh. EF generate inner join.
class CarSalesBN
{
CarSalesElh CarSalesElhRef
}
when i inverse relation, my problem been solved and EF genarte left outer join.
my question is that, why EF Use Association to generate SQL Query, I am not used navigation properties in Linq Query and not see any related document in MSDN for that.
- Moved by CoolDadTx Monday, December 21, 2015 4:17 PM EF related
Monday, December 21, 2015 12:19 PM
Answers
-
Sorry, I don't follow your question.
It's inferring how the relationship is supposed to work.
What I'm saying is if you want to specify something as being 0:m you should explicitly use HasOptional with code first.
Did you follow the link and read the article?
Monday, December 21, 2015 9:05 PM
All replies
-
You should use hasrequired and hasoptional as explained here to specify the nature of relationships:
modelBuilder.Entity<Department>() .HasOptional(x => x.Administrator);
I think if you don't then it can be inferred from the order of the relationships.Monday, December 21, 2015 12:28 PM -
ok, You say that I can change relationship, but my Question is that Are relationship has impact on GroupJoin?.I am not use navigation property in Linq Query but it check and leverage relationship among two enties.
- Edited by Seyyed Hossein Razzaghi Monday, December 21, 2015 1:21 PM
Monday, December 21, 2015 1:17 PM -
Sorry, I don't follow your question.
It's inferring how the relationship is supposed to work.
What I'm saying is if you want to specify something as being 0:m you should explicitly use HasOptional with code first.
Did you follow the link and read the article?
Monday, December 21, 2015 9:05 PM -
If there is not data annotation attributes and Fluent API along with the data class.
then, Data classes conveniently follow code first convention to help EF work with them
https://msdn.microsoft.com/en-us/data/jj591583.aspx
https://msdn.microsoft.com/en-us/data/jj679962.aspx
DON'T TRY SO HARD,THE BEST THINGS COME WHEN YOU LEAST EXPECT THEM TO.
Saturday, December 26, 2015 6:44 AM