EF Core Join from two IQueryables throws System.ArgumentNullException

asp.net-core c# ef-core-2.1 entity-framework-core linq


Having System.ArgumentNullException when trying to Join with ef core from two datasets.

I'm using asp.netcore's built in Identity feature and I have two contexts: MyDbContext and IdentityDbContext. I have a User entity in MyDbContext which holds an IdentityGuid, so I could know which user is associated to which IdentityUser.

I would like to do a Join and get a KeyValuePair and provide that as a ViewModel.

        var users = _context.Users;
        var identityUsers = _identityContext.Users;

        var selection = users.Join(identityUsers,
            u => u.IdentityGuid,
            iu => iu.Id,
            (u, iu) => new KeyValuePair<User, IdentityUser>(u, iu));

        return View(await selection.ToListAsync());

Executing that Join throws System.ArgumentNullException, however if I will do a ToList() on both datasets before Join then it works just fine. Here is the Exception:

ArgumentNullException: Value cannot be null. Parameter name: entityType Microsoft.EntityFrameworkCore.Utilities.Check.NotNull(T value, string parameterName)

12/20/2018 12:17:58 PM

Accepted Answer

As Miamy mentioned in his comment, EF Core does not support cross-context deferred execution joins. One (or both) of the them will need to have .ToList() called to cause them to both be evaluated and pulled into local memory for the join.

This could also be done in theory by writing the join sql manually and using EF to execute (as this would allow the cross join) but generally the solution should be to pull the data into into local with a .ToList() and then join.

It's an unfortunate thing that cross joins like that arent supported, though I dont believe they are in NHibernate either, so its an all round limitation to my knowledge

12/20/2018 12:46:16 PM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow