【问题标题】:.ThenInclude in Specification Pattern , EF Core.然后包含在规范模式,EF Core
【发布时间】:2021-06-16 05:25:19
【问题描述】:

谁能指导我,如何在规范模式中添加 .ThenInclude(s) ;我正在使用实体框架核心。

我有下面的 .Include 代码,它正在工作。

public interface ISpecification<T>
{
    Expression<Func<T, bool>> Criteria { get; }
    List<Expression<Func<T, object>>> Includes { get; }
}

public abstract class BaseSpecification<T> : ISpecification<T>
{
    protected BaseSpecification(Expression<Func<T, bool>> criteria)
    {
        Criteria = criteria;
    }
    public Expression<Func<T, bool>> Criteria { get; }
    public List<Expression<Func<T, object>>> Includes { get; } = new List<Expression<Func<T, object>>>();
    public List<string> IncludeStrings { get; } = new List<string>();

    protected virtual void AddInclude(Expression<Func<T, object>> includeExpression)
    {
        Includes.Add(includeExpression);
    }
}

}

我们将不胜感激。

【问题讨论】:

    标签: c# entity-framework-core specification-pattern


    【解决方案1】:

    找到解决上述问题的方法。下面是解决方案。添加BaseSpecification类

    protected virtual void AddInclude(string includeString)
    {
        IncludeStrings.Add(includeString);
    }
    

    之后在规范过滤器中使用如下所示的然后包含。

     AddInclude("Contacts.PrefixTitle");
    

    这将包括主要类别中的联系人,即我的案例中的帐户,然后包括联系人,然后是 PrefixTitle

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-12
      • 2017-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多