entity-framework-mock icon indicating copy to clipboard operation
entity-framework-mock copied to clipboard

Multiple left joins in query causes NullReferenceException

Open DreadedTuba opened this issue 1 year ago • 1 comments

I have a code like this where a query has multiple left joins and it throwing exception when executed. any know what I'm doing wrong here? using version 1.0.0.67

public DbContextMock<MyDbContext> mockDb;
public DbContext db => mockDb.Object;
mockDb.CreateDbSetMock(a => a.TableA);
mockDb.CreateDbSetMock(a => a.TableB);
mockDb.CreateDbSetMock(a => a.TableC);

var query2  = from a in db.TableA
                     join b in db.TableB on a.Id equals b.TableAId into bb
                    from b in bb.DefaultIfEmpty()
                    select a;
var results2 = query.ToList();
// this results2 works fine, no exception

var query3  = from a in db.TableA
                     join b in db.TableB on a.Id equals b.TableAId into bb
                    from b in bb.DefaultIfEmpty()
                     join c in db.TableC on b.Id equals c.TableBId into cc
                    from c in cc.DefaultIfEmpty()
                    select a;
var results3 = query.ToList();
// this results3 throw NullReferenceException; Object referense not set to instance of an object
// the only change is to add another left join 

DreadedTuba avatar Feb 23 '24 16:02 DreadedTuba

nevermind, I found the answer https://stackoverflow.com/questions/26634452/linq-query-nullreferenceexception-on-multiple-cascade-left-joins

DreadedTuba avatar Feb 23 '24 17:02 DreadedTuba