コード例 #1
0
        public IEnumerable <T> GetAll()
        {
            using (IServiceScope scope = _provider.GetRequiredService <IServiceScopeFactory>().CreateScope())
            {
                KlanikContext _context = _provider.GetService <KlanikContext>();

                return(_context.Set <T>().ToList());
            }
        }
コード例 #2
0
        public T GetById(Guid id)
        {
            using (IServiceScope scope = _provider.GetRequiredService <IServiceScopeFactory>().CreateScope())
            {
                KlanikContext _context = _provider.GetService <KlanikContext>();

                var set = _modifiers.Aggregate((IQueryable <T>)_context.Set <T>(),
                                               (current, include) => current.Include(include));

                return(set.FirstOrDefault(x => x.Id == id));
            }
        }
コード例 #3
0
        public void Delete(T toRemove)
        {
            using (IServiceScope scope = _provider.GetRequiredService <IServiceScopeFactory>().CreateScope())
            {
                KlanikContext _context = _provider.GetService <KlanikContext>();

                var exists = _context.Set <T>().Any(x => x.Id == toRemove.Id);

                if (exists)
                {
                    _context.Entry(toRemove).State = EntityState.Deleted;
                }

                _context.SaveChanges();
            }
        }
コード例 #4
0
        public void Create(T toCreate)
        {
            using (IServiceScope scope = _provider.GetRequiredService <IServiceScopeFactory>().CreateScope())
            {
                KlanikContext _context = _provider.GetService <KlanikContext>();

                var exists = _context.Set <T>().Any(x => x.Id == toCreate.Id);

                _context.Entry(toCreate).State =
                    exists ?
                    EntityState.Modified :
                    EntityState.Added;

                _context.SaveChanges();
            }
        }