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