I'm getting "specified method is not supported" when I include a specific navigation property with e
I'm using ASP.NET Core Identity with a custom IdentityUser that looks like this:
public class ApplicationUser : IdentityUser
{
public string? FirstName { get; set; }
public string? LastName { get; set; }
public IEnumerable<Subject> Subjects { get; set; } = Enumerable.Empty<Subject>();
}
My Subject class looks like this:
public class Subject : BaseEntity
{
public string Name { get; set; } = "";
public ICollection<ApplicationUser> Users { get; set; } = new List<ApplicationUser>();
}
It's a m:n relationship that is working fine. I just can't include Subjects from my ApplicationUser and I can't include Users from my Subject. I can include everything else, just not that. I always get following error:
System.NotSupportedException: Specified method is not supported.
I am including it like this:
await this._context.Subjects.Include(subj => subj.Users).ToListAsync()
await this._context.Users.Include(user => user.Subjects).ToListAsync();
However whenever I use AutoMappers ProjectTo extension, it somehow works:
await this._context.Set<ApplicationUser>()
.Include(user => user.Subjects)
.ProjectTo<UnoccupiedStudentDto>(this._mapper.ConfigurationProvider)
.ToListAsync(cancellationToken);
What am I missing here?
5 Replies
@dancis Try changing the subject property to something like this I believe there is no
Include
method extension on IEnumerable
. Although I haven't checked.Just as your message came in, I changed it up to virtual ICollection and that worked! I don't know, why I used a IEnumerable and somehow didn't think that would make a difference
That's probably the issue, I don't think there is an Include extension method on IEnumerable.
@dancis Also apply the
AsNoTracking
extension. So that the context doesn't keep track of the entities state. Because I assume you are just getting them and won't do any modifications.Good point, will do that. Thanks
Don't use
virtual
There's no point if you don't use lazy loading
And if you do use lazy loading, you shouldn't
Also, I don't think .Include()
is needed for the mapper to work. And if it is, then it's a good excuse to get rid of it and write your mappings manually
Also also, after mapping is done, you get a different entity
It's not tracked in the change tracker. It cannot be tracked in the change tracker
So a .Select()
implies no-tracking
So should .ProjectTo()