EF 通用存储库多个包含 [英] EF Generic Repository Multiple Includes

查看:20
本文介绍了EF 通用存储库多个包含的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想法是拥有一个适用于所有实体的通用存储库.我做到了,但是如果我需要一个方法来包含一个或多个其他实体,那对我来说就有问题了.我在代码中加入了一些想法,但这对我不起作用.我也一直在考虑在 EF 中使用聚合函数,但我从未使用过.有人可以指导我如何管理这个吗?

Idea is to have one Generic Repository which will work with all entities. I managed that, but if I need to have method which should include one or more other entities there is a problem for me. I have put some idea in code, but that is not working for me. Also I have been thinking to use aggregate function in EF, but that I have never use. Can someone give me direction how I can manage this ?

  public interface IRepository<T> where T : BaseEntity
    {
        IEnumerable<T> GetAll();
        T Get(Int64 id);
        void Insert(T entity);
        void Delete(T entity);
        Task<bool> SaveChangesAsync();
        T SearchByName(Expression<Func<T, bool>> predicate);
        IEnumerable<T> GetAll(string[] includes);

    }



public class Repository<T> : IRepository<T> where T : BaseEntity
    {
        private Entities.AppContext _context;
        private DbSet<T> entities;

        public Repository(Entities.AppContext context)
        {
            _context = context;
            entities = _context.Set<T>();
        }

        public void Delete(T entity)
        {
            if (entity == null)
            {
                throw new ArgumentNullException("entity");
            }
            entities.Remove(entity);
        }

        public T Get(long id)
        {
            return entities.SingleOrDefault(s => s.Id == id);
        }

        public IEnumerable<T> GetAll()
        {
            return entities.ToList();
        }

        public IEnumerable<T> GetAll(string[] includes)
        {
            foreach (string include in includes)
            {
                entities.Include(include);
            }
            return entities;
        }

        public void Insert(T entity)
        {
            if (entity == null)
            {
                throw new ArgumentNullException("entity");
            }
            entities.Add(entity);
        }

        public async Task<bool> SaveChangesAsync()
        {
            try
            {
                return (await _context.SaveChangesAsync()) > 0;
            }
            catch (Exception ex)
            {

                return false;
            }

        }

        public T SearchByName(Expression<Func<T, bool>> predicate)
        {
            return entities.Where(predicate).SingleOrDefault();
        }
    }

推荐答案

您陷入了调用返回某些内容而忽略结果的方法的典型陷阱.行 entities.Include(include); 什么都不做 - 类似于 entities.Where(...);, entities.Select(...);

You have fallen in the typical trap of calling a method which returns something and ignoring the result. The line entities.Include(include); does nothing - similar to entities.Where(...);, entities.Select(...); etc.

正确的代码是这样的:

var query = entities.AsQueryable();
foreach (var include in includes)
    query = query.Include(include);
return query;

或使用单行 Aggregate:

or with single line Aggregate:

return includes.Aggregate(entities.AsQueryable(), (query, path) => query.Include(path));

这篇关于EF 通用存储库多个包含的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆