如何在EF Core 2中编写.ThenInclude的存储库方法

编程入门 行业动态 更新时间:2024-10-22 19:31:12
本文介绍了如何在EF Core 2中编写.ThenInclude的存储库方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试为Entity Framework Core 2.0编写一个存储库方法,该方法可以使用.ThenInclude处理返回的属性的子级集合,但是第二个表达式存在问题。这是.include的工作方法,该方法将返回实体的子属性(您提供lambda列表)。

I'm trying to write a repository method for Entity Framework Core 2.0 that can handle returning child collections of properties using .ThenInclude, but I'm having trouble with the second expression. Here is a working method for .Include, which will return child properties (you supply a list of lambdas) of your entity.

public T GetSingle(Expression<Func<T, bool>> predicate, params Expression<Func<T, object>>[] includeProperties) { IQueryable<T> query = _context.Set<T>(); foreach (var includeProperty in includeProperties) { query = query.Include(includeProperty); } return query.Where(predicate).FirstOrDefault(); }

现在这是我尝试编写一个将两个元组合并为一个的方法的尝试表达式并将它们提供给.Include(a => a.someChild).ThenInclude(b => b.aChildOfSomeChild)链。这不是一个完美的解决方案,因为它只能处理一个孩子中的一个孩子,但这只是一个开始。

Now here is my attempt at writing a method that will take a Tuple of two Expressions and feed those into a .Include(a => a.someChild).ThenInclude(b => b.aChildOfSomeChild) chain. This isn't a perfect solution because it only handles one child of a child, but it's a start.

public T GetSingle(Expression<Func<T, bool>> predicate, params Tuple<Expression<Func<T, object>>, Expression<Func<T, object>>>[] includeProperties) { IQueryable<T> query = _context.Set<T>(); foreach (var includeProperty in includeProperties) { query = query.Include(includeProperty.Item1).ThenInclude(includeProperty.Item2); } return query.Where(predicate).FirstOrDefault(); }

Intellisense返回错误,提示无法从用法中推断出类型,请尝试明确指定类型。我觉得这是因为Item2中的表达式需要分类为与Item1相关,因为它需要知道它与孩子之间的关系。

Intellisense returns an error saying "The type cannot be inferred from the usage, try specifying the type explicitly". I have a feeling it's because the expression in Item2 needs to be classified as somehow related to Item1, because it needs to know about the child relationship it has.

编写这样的方法有什么想法或更好的技巧吗?

Any ideas or better techniques for writing a method like this?

推荐答案

我在线上找到了这个存储库方法,它确实满足我的要求。 Yared的回答很好,但并非始终如此。

I found this repository method online and it does exactly what I wanted. Yared's answer was good, but not all the way there.

/// <summary> /// Gets the first or default entity based on a predicate, orderby delegate and include delegate. This method default no-tracking query. /// </summary> /// <param name="selector">The selector for projection.</param> /// <param name="predicate">A function to test each element for a condition.</param> /// <param name="orderBy">A function to order elements.</param> /// <param name="include">A function to include navigation properties</param> /// <param name="disableTracking"><c>True</c> to disable changing tracking; otherwise, <c>false</c>. Default to <c>true</c>.</param> /// <returns>An <see cref="IPagedList{TEntity}"/> that contains elements that satisfy the condition specified by <paramref name="predicate"/>.</returns> /// <remarks>This method default no-tracking query.</remarks> public TResult GetFirstOrDefault<TResult>(Expression<Func<TEntity, TResult>> selector, Expression<Func<TEntity, bool>> predicate = null, Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null, Func<IQueryable<TEntity>, IIncludableQueryable<TEntity, object>> include = null, bool disableTracking = true) { IQueryable<TEntity> query = _dbSet; if (disableTracking) { query = query.AsNoTracking(); } if (include != null) { query = include(query); } if (predicate != null) { query = query.Where(predicate); } if (orderBy != null) { return orderBy(query).Select(selector).FirstOrDefault(); } else { return query.Select(selector).FirstOrDefault(); } }

用法:

var affiliate = await affiliateRepository.GetFirstOrDefaultAsync( predicate: b => b.Id == id, include: source => source .Include(a => a.Branches) .ThenInclude(a => a.Emails) .Include(a => a.Branches) .ThenInclude(a => a.Phones));

更多推荐

如何在EF Core 2中编写.ThenInclude的存储库方法

本文发布于:2023-11-17 04:56:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1608910.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方法   如何在   Core   EF   ThenInclude

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!