Robbie Mills
Robbie Mills

Reputation: 2945

The property '' on entity type could not be found since upgrading to EF 2.1

I've upgraded my app to Ef Core 2.1 and a query that used to work in EF 2.0 is now giving errors.

I use variations of the same query across multiple different tables and they are all failing with the same error now that I've upgraded

Here are my models:

public class Promotion  
{
    public int PromotionId { get; set; }   
    // I've removed the other fields  

    public virtual ICollection<PromotionTopicJoin> PromotionTopicJoins {get; set;}

}

public class PromotionTopic  
{
    public int PromotionTopicId { get; set; }     

    [MaxLength(50)]
    [Required]
    public string Name { get; set; }

    public virtual ICollection<PromotionTopicJoin> PromotionTopicJoins {get; set;}


}

public class PromotionTopicJoin
{
    public int PromotionId { get; set; }
    public Promotion Promotion { get; set; }
    public int PromotionTopicId { get; set; }
    public PromotionTopic PromotionTopic { get; set; }

}

And this is the query that I run:

    return await _context.PromotionTopics
        .Where(p => p.PromotionTopicId == TopicId)
        .SelectMany(p => p.PromotionTopicJoins)
        .Select(pc => pc.Promotion)
        .Select(p => new PromotionDTO
        {
            PromotionId = p.PromotionId,
            // I've removed all the other fields
            Topics = p.PromotionTopicJoins.Select(itj => itj.PromotionTopic.Name).ToList()

        })
        .Where(x => x.Is_Active)
        .OrderByDescending(x => x.Created)
        .ToListAsync();

The stacktrace is quite large, so I've only included the error and the preceding line.

System.InvalidOperationException: The property '' on entity type 'PromotionTopicJoin' could not be found. Ensure that the property exists and has been included in the model. at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.Internal.MemberAccessBindingExpressionVisitor.GetPropertyPath(Expression expression, QueryCompilationContext queryCompilationContext, out QuerySourceReferenceExpression querySourceReferenceExpression)

FYI, my versions of Microsoft.EntityFrameworkCore and Microsoft.EntityFrameworkCore.SqlServer went from 2.0.1 to 2.1.0

Upvotes: 1

Views: 2320

Answers (1)

Ivan Stoev
Ivan Stoev

Reputation: 205849

Unfortunately this is a clear indication of EF Core query translation regression bug.

As such, all you can do is to report it to EF Core Issue Tracker. Provide the same minimal repro (remove the .Where(x => x.Is_Active) and .OrderByDescending(x => x.Created) - they don't compile currently, but the issue reproduces w/o them. Also reproduces with ToList, so it's not async related).

As a workaround until the issue is fixed, start the query directly from the join entity (the problem seems to be related to SelectMany navigation):

return await _context.Set<PromotionTopicJoin>()
    .Where(pc => pc.PromotionTopicId == TopicId)
    .Select(pc => pc.Promotion)
    .Select(p => new PromotionDTO
    {
        PromotionId = p.PromotionId,
        // I've removed all the other fields
        Topics = p.PromotionTopicJoins.Select(itj => itj.PromotionTopic.Name).ToList()    
    })
    .Where(x => x.Is_Active)
    .OrderByDescending(x => x.Created)
    .ToListAsync(); 

Upvotes: 3

Related Questions