Entity framework core 实体框架核心5.0-多对多选择查询

Entity framework core 实体框架核心5.0-多对多选择查询,entity-framework-core,include,many-to-many,Entity Framework Core,Include,Many To Many,我正在尝试获取一个用户,其中包含一个项目列表,映射了一个多对多实体用户项目。但是,由于无法解决的错误(问题底部的错误),我无法检索映射的项目。这是我的密码: public class User { public int Id { get; set; } public ICollection<UserItem> UserItems { get; set; } } public class Item { public int Id { get; set; }

我正在尝试获取一个
用户
,其中包含一个
项目列表
,映射了一个多对多实体
用户项目
。但是,由于无法解决的错误(问题底部的错误),我无法检索映射的项目。这是我的密码:

public class User
{
    public int Id { get; set; }
    public ICollection<UserItem> UserItems { get; set; }
}

public class Item
{
    public int Id { get; set; }
    public ICollection<UserItem> UserItems { get; set; }
}

public class UserItem
{
    public int Id { get; set; }

    public int UserId { get; set; }
    public User User { get; set; }

    public int ItemId { get; set; }
    public Item Item { get; set; }

    public int Quantity { get; set; }
}
我有以下使用此方法的通用回购:

public class GenericRepository<T> : where T : class
{
    private readonly DbContext _context;

    public GenericRepository(DbContext context) => _context = context;

    public T Get(Expression<Func<T, bool>> where, params Expression<Func<T, object>>[] navigationProperties)
    {
        IQueryable<T> query = _context.Set<T>();
        query = navigationProperties.Aggregate(query, (current, property) => current.Include(property));
        var entity = query.FirstOrDefault(where);
        return entity;
    }
 }
错误:

System.InvalidOperationException: 'The expression 'x.UserItems.AsQueryable().Select(y => y.Item)' is invalid inside an 'Include' operation, since it does not represent a property access: 't => t.MyProperty'. To target navigations declared on derived types, use casting ('t => ((Derived)t).MyProperty') or the 'as' operator ('t => (t as Derived).MyProperty'). Collection navigation access can be filtered by composing Where, OrderBy(Descending), ThenBy(Descending), Skip or Take operations. For more information on including related data, see http://go.microsoft.com/fwlink/?LinkID=746393.'

我做错了什么,这似乎适用于我的其他项目?

发生此错误的原因是您没有传入导航属性(
x.UserItems
将是导航属性),而是要传入导航属性
UserItems.Select(y=>y.Item)
不是x的属性,因为
Select()
是一个函数,因此不能包含它

您试图做的(我假设它包括UserItems和相应的项)不适用于存储库的当前实现。要包含导航属性的导航属性,必须使用include(),而不是
。include()
,它仅适用于为其创建数据库集的实体上直接定义的导航属性


但除了您的问题之外,我建议不要使用这样一个存储库的通用实现。使用reposiories的主要好处是将与实体加载和存储相关的代码与代码的其余部分分离。在您的案例中,如果存储库的使用者知道必须包括导航属性,并且他必须提供这些属性,那么拥有存储库有什么意义呢?然后,消费者再次关心特定于数据库的代码,这使得存储库变得不必要。我建议只制作一个conrete“UserRepository”,它只能用于检索用户,并显式地包含所需的属性。

您需要这样一个函数来做什么?在现实生活中,所有谓词都是无用的。
    var user = repo.Get(x => x.Id == 1, x => x.UserItems.Select(y => y.Item));
System.InvalidOperationException: 'The expression 'x.UserItems.AsQueryable().Select(y => y.Item)' is invalid inside an 'Include' operation, since it does not represent a property access: 't => t.MyProperty'. To target navigations declared on derived types, use casting ('t => ((Derived)t).MyProperty') or the 'as' operator ('t => (t as Derived).MyProperty'). Collection navigation access can be filtered by composing Where, OrderBy(Descending), ThenBy(Descending), Skip or Take operations. For more information on including related data, see http://go.microsoft.com/fwlink/?LinkID=746393.'