I have the following schema:
public class Post {
public string Id {get;set;}
public string Content {get;set;}
public AppUser AppUser {get;set;}
public List<PostTag> PostTags {get;set;}
}
public class Tag {
public string Id {get;set;}
public string Name {get;set;}
public List<PostTag> PostTags {get;set;}
}
public class PostTag
{
public string PostId { get; set; }
public Post Post { get; set; }
public string TagId { get; set; }
public Tag Tag { get; set; }
}
With the following db relationships:
builder.Entity<PostTag>()
.HasKey(x => new { x.PostId, x.TagId });
builder.Entity<PostTag>()
.HasOne(st => st.Post)
.WithMany(s => s.PostTags)
.HasForeignKey(st => st.PostId);
builder.Entity<PostTag>()
.HasOne(st => st.Tag)
.WithMany(s => s.PostTags)
.HasForeignKey(st => st.TagId);
I'm writing a query to get all the Posts that are linked to a specific Tag, based on the provided TagId.
First I get all the posts using:
var postsQuery = _ctx.PostTag
.Include(st => st.Post)
.Where(st => st.TagId == {provided TagId})
.Select(st => st.Post);
Since I want to include some further data to each post, I do:
var postsWithExtraData = postsQuery
.Include(s => s.AppUser)
.Include(s => s.PostTags)
.ThenInclude(st => st.Tag)
.OrderByDescending(s => s.TotalVoteCount)
.ToListAsync();
But the query breaks on the first .Include with this error:
EF Core “InvalidOperationException: Include has been used on non entity queryable”
Why is this happening and how can I fix it?
EDIT: Potential solution that I got to work:
var posts = _ctx.PostTag
.Include(st => st.Post)
.ThenInclude(s => s.AppUser)
.Include(st => st.Post)
.ThenInclude(s => s.PostTags)
.ThenInclude(st => st.Tag)
.Where(st => st.TagId == request.TagId)
.Select(st => st.Post)
.ToList();
Would love to know if this is a good approach or not.
AppUsercome from? It's not in any of the code you show.postsQueryis aIEnumerable<Post>- at that point, you're no longer in LINQ-to-Entities (you're not querying the database), so you can't call the.Includemethod.IQuerable<Post>for me..Select()method, you can't use Include anymore - Select tells EF to translate the LINQ expression to SQL. I wish this had more details, but basically: learn.microsoft.com/en-us/ef/core/querying/client-eval