简体   繁体   English

多对多使用 LinqKit 和 EF Core 和 Generic 类

[英]Many to Many using LinqKit with EF Core with Generic class

I'm currently converting an EF project to EF Core but I don't know how or whether it is possible to:我目前正在将 EF 项目转换为 EF Core,但我不知道如何或是否可以:

a) Use LinqKit on Many-To-Many with EF Core a) 使用 EF Core 在LinqKit上使用LinqKit

b) Use a) with a Generic class b) 将 a) 与泛型类一起使用

I'm currently using the following class:我目前正在使用以下课程:

    protected virtual IQueryable<TEntity> GetQueryable(
        Expression<Func<TEntity, bool>> filter = null,
        Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
        string includeProperties = null,
        bool isCollection = false,
        int? skip = null,
        int? take = null)
    {
        IQueryable<TEntity> query = this.Context.Set<TEntity>();

        if (filter != null)
        {
            query = query.AsExpandable().Where(filter);
        }

        if (includeProperties != null)
        {
            foreach (var includeProperty in includeProperties.Split
                (new[] { ',' }, 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;
    }

In EF 6, I would set the type of my generic class to a main Entity I wanted to deal with and if it had a many to many, I would simply set the IncludeProperties to the child Entity.在 EF 6 中,我将通用类的类型设置为我想要处理的主实体,如果它有多个,我只需将 IncludeProperties 设置为子实体。

For example:例如:

var test = GetQueryable<Company>(null, "Users");

This would returns all companies and return all the users associated with each relevant company.这将返回所有公司并返回与每个相关公司关联的所有用户。

LinqKit would then come in to add additional filters, orders, etc...然后,LinqKit 会进来添加额外的过滤器、订单等......

But I cannot figure out how to use this with a Many to Many relation in EF Core.但我无法弄清楚如何在 EF Core 中使用多对多关系使用它。

I've seen an example where you do something similar to this:我看过一个例子,你做类似的事情:

this.Context.Companies.Include("CompanyUsers")
                      .ThenInclude("Users")
                      .ToListAsynch()

And the above looks like what I need but I need to use it to be generic and I need to apply filters with LinqKit or some other way but I need to set filters, sorting, paging, etc... dynamically.上面看起来像我需要的,但我需要使用它来通用,我需要使用 LinqKit 或其他方式应用过滤器,但我需要动态设置过滤器、排序、分页等。

Any ideas?有任何想法吗?

Thanks谢谢

UPDATE-1:更新-1:

My many-to-many relation class is defined as follows:我的多对多关系类定义如下:

public class CompanyUsers
{
    public Guid UserId { get; set; }

    public User User { get; set; }

    public Guid CompanyId { get; set; }

    public Company Company { get; set; }
}

The reason I'm adding this is that while my table of users is called "Users" the navigation property is called User and as suggested by Dennis, I should specify the Many-To-Many class (CompanyUsers) followed by the navigation property (User) so rather than being:我添加这个的原因是,虽然我的用户表被称为“用户”,但导航属性被称为用户,正如丹尼斯所建议的那样,我应该指定多对多类(公司用户),然后是导航属性(用户)所以而不是:

var test = GetQueryable<Company>(null, "CompanyUsers.Users");

it should be defined as instead:它应该被定义为:

var test = GetQueryable<Company>(null, "CompanyUsers.User");

Note the s was removed to match the navigation property rather than the table.请注意, s已被删除以匹配导航属性而不是表格。

The simplest solution would be to use the string overload of the Include .*最简单的解决方案是使用Include .* 的字符串重载

var test = GetQueryable<Company>(null, "CompanyUsers.User");

*I don't recommend using magic strings. *我不建议使用魔法字符串。 You should consider writing an method that returns this based on the models properties.您应该考虑编写一个基于模型属性返回 this 的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM