Association with CanBeNull = false produces LeftJoin when using QueryExpressionMethod
See original GitHub issueLinqToDb-Version: 3.1.0 DB: Firebird 2.1
Hi,
I want to generate an inner join by using the association attribute and a QueryExpressionMethod.
Let’s say I have this structure:
public class Card{
public int Id { get; set;}
public string CardName { get; set;}
[Association(QueryExpression = nameof(Card_Owner), CanBeNull = false)]
public Client Owner {get; set; }
public int OwnerId {get; set; }
public static Expression<Func<Card, IDataContext, IQueryable<Client>>> Card_Owner()
{
return (c, db) => db.GetTable<Client>().Where(cl=> cl.Id == c.OwnerId);
}
}
public class Client{
public int Id { get; set;}
public string Name {get; set; }
}
So if I call it :
using(var db = new MyDb()){
var cards = db.GetTable<Card>().LoadWith(x => x.Client).ToList();
)
The resulting SQL produces a left join.
if I change the Association Attribute to :
public class Card{
public int Id { get; set;}
public string CardName { get; set;}
[Association(ThisKey = "OwnerId", OtherKey = "Id", CanBeNull = false)]
public Client Owner {get; set; }
public int OwnerId {get; set; }
}
it produces an inner join. Although both have the CanBeNull set to false!
Issue Analytics
- State:
- Created 3 years ago
- Comments:8 (5 by maintainers)
Top Results From Across the Web
No results found
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Yes, in this case we ignore CanBeNull attribute. Will check what we can do here. But you have to understand if we use INNER JOIN, then association may filter out principal entities.
It might not be such an issue if the entities models correctly use Nullable Reference Types for association properties.
If the helper methods & code refactoring proposed in issue https://github.com/linq2db/linq2db/issues/2592 are accepted in to Linq2DB, then the canBeNull attribute could possibly be removed altogether.
Then the correct joins would be used purely based on the if the association properties in the entities model are nullable: