【发布时间】:2017-09-01 12:47:57
【问题描述】:
我的 IQueryable 看起来像这样:
IQueryable<TEntity> query = context.Set<TEntity>();
query = query.Include("Car").ThenInclude("Model");
“IQueryable”不包含“ThenInclude”的定义 并且没有扩展方法“ThenInclude”接受第一个参数 可以找到类型“IQueryable”(您是否缺少使用 指令还是程序集引用?)
我有所有需要的参考资料:
using Content.Data.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
为什么它不识别 ThenInclude?p>
查询:
[Content.Data.Models.Article]).Where(x => (((x.BaseContentItem.SiteId == value(Content.Business.Managers.ArticleManager+<>c__DisplayClass8_0).id) AndAlso x.BaseContentItem.IsActive) AndAlso x.BaseContentItem.IsLive)).Include("BaseContentItem").Include("BaseContentItem.TopicTag").Include("MainImage")}
在我包含 .Include("BaseContentItem.TopicTag") 部分后失败。
所以我刚刚读到,使用通用存储库你会丢失 ThenInclude。我正在使用这个通用代表:
public class ReadOnlyRepository<TContext> : IReadOnlyRepository
where TContext : DbContext
{
protected readonly TContext context;
public ReadOnlyRepository(TContext context)
{
this.context = context;
}
private IQueryable<TEntity> GetQueryable<TEntity>(
Expression<Func<TEntity, bool>> filter = null,
Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
string includeProperties = null,
int? skip = null,
int? take = null)
where TEntity : class, IEntity
{
includeProperties = includeProperties ?? string.Empty;
IQueryable<TEntity> query = context.Set<TEntity>();
if (filter != null)
{
query = query.Where(filter);
}
foreach (var includeProperty in includeProperties.Split
(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
{
query = query.Include(includeProperty);
}
if (orderBy != null)
{
query = orderBy(query);
}
if (skip.HasValue)
{
query = query.Skip(skip.Value);
}
if (take.HasValue)
{
query = query.Take(take.Value);
}
return query;
}
【问题讨论】:
-
EF Core 是否还有
.Include(string)和.ThenInclude(string)方法?不应该是query = query.Include(e => e.Car).ThenInclude(e => e.Model);吗? -
@DavidG 确实。或者只是
Include(dot_separated_string)。但是 OP 声称它不起作用,而且我不记得从引入重载(如果我没记错的话是 1.1.0)时点分隔的属性名称Include有问题。 -
@IvanStoev 啊,是的,这是可能的。从来都不是基于字符串的版本的粉丝,因为它们缺少编译时类型检查。
-
编辑后不清楚你在问什么。
标签: c# entity-framework linq entity-framework-core