PawelGerr/Thinktecture.EntityFrameworkCore

Joining with EF Core 5 and TempTables gets error

stevencasburn opened this issue · 3 comments

partial void OnModelCreatingPartial(ModelBuilder modelBuilder)
        {
            modelBuilder.ConfigureTempTable<long>();
.....

In my Context I configuered my TempTables to join some IDs with.

var rightIdTempTableTask = Context.BulkInsertValuesIntoTempTableAsync(rightIds);
	rightIdTempTableTask.Wait();
	var rightIdTempTable = rightIdTempTableTask.Result.Query;

var articleNumbers = Context.ArticleNumbers
	.Include(an => an.Article.Producer)
		.ThenInclude(p => p.ProducerType)
	.Include(an => an.Article)
		.ThenInclude(a => a.ArticleDescriptions)
		.ThenInclude(ad => ad.Language)
	.Join(Context.ArticleRights,
		an => an.ArticleId,
		ar => ar.ArticleId,
		(an, ar) => new { an, ar })
	.Join(rightIdTempTable,
		x => x.ar.RightId,
		rid => rid.Column1,
		(x, rid) => x.an)
	.Where(an => articleNumbersReduced == an.ArticleNumberReduced)
	.Where(an => an.Article.ProducerId == producerId)
	.AsNoTracking()
	.ToList();

When using the code above, I'll get the following exception:

System.InvalidOperationException: 'Unable to translate collection subquery in projection since the parent query doesn't project key columns of all of it's tables which are required to generate results on client side. This can happen when trying to correlate on keyless entity or when using 'Distinct' or 'GroupBy' operations without projecting all of the key columns.'

ArticleNumbers and all included tables do have a [Key] annotation on their Id column.

Commenting out Join(rightIdTempTable...) and the code works.

Code was working a while ago.

Any ideas what's wrong here?

Hi, I wasn't able to reproduce the issue on my own. Could you provide a small repro?

According to the github issue 23830, the exception is raised due to some limitations of EF when using Inlcude and ThenInclude along with keyless entities - and the temp table entities are keyless by default.

You can configure the temp table so it has a key.

modelBuilder.ConfigureTempTable<long>(isKeyless: false);

Hi Pawel, sorry for being too busy to reply. I've solved the problem now with a good workaround, but I will give it a try soon. I guess it might be the solution. Thanks for the quick response!