/// <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>()); }
protected Repository(PatientContext context) { Context = context; DbSet = Context.Set <TEntity>(); }
public async Task <int> AddAsync(Patient entity) { await DbContext.Set <Patient>().AddAsync(entity); return(await DbContext.SaveChangesAsync()); }