示例#1
0
        /// <summary>
        /// Gets the subset of <typeparamref name="TEntity"/> entities corresponding to the predicate <paramref name="predicate"/>.
        /// </summary>
        /// <param name="predicate">Filter which entities are returned.</param>
        /// <param name="eager">Indicates whether or not to eagerly load navigation properties.</param>
        /// <returns></returns>
        public async Task <IEnumerable <TEntity> > GetByConditionAsync(Expression <Func <TEntity, bool> > predicate, bool eager = false)
        {
            if (predicate == null)
            {
                throw new ArgumentNullException(nameof(predicate));
            }

            var query = _context.Set <TEntity>().AsQueryable();

            query = query.Where(predicate);

            if (eager)
            {
                var navigations = _context.Model.FindEntityType(typeof(TEntity))
                                  .GetDerivedTypesInclusive()
                                  .SelectMany(type => type.GetNavigations())
                                  .Distinct();

                foreach (var property in navigations)
                {
                    query = query.Include(property.Name);
                }
            }

            var results = await query.ToListAsync();

            return(results ?? new List <TEntity>());
        }
示例#2
0
 protected Repository(PatientContext context)
 {
     Context = context;
     DbSet   = Context.Set <TEntity>();
 }
示例#3
0
        public async Task <int> AddAsync(Patient entity)
        {
            await DbContext.Set <Patient>().AddAsync(entity);

            return(await DbContext.SaveChangesAsync());
        }