Пример #1
0
        /// <summary>
        /// Deletes entity
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task <bool> Delete(int id)
        {
            using (TodoAppDbContext context = _contextFactory.CreateDbContext())
            {
                T entity = context.Set <T>().FirstOrDefault((e) => e.Id == id);
                context.Set <T>().Remove(entity);
                await context.SaveChangesAsync();

                return(true);
            }
        }
Пример #2
0
        private static async Task ResetTable <TEntity>(List <TEntity> entities) where TEntity : BaseEntity
        {
            IConfiguration configuration = new ConfigurationBuilder()
                                           .SetBasePath(Directory.GetCurrentDirectory())
                                           .AddJsonFile("appsettings.Development.json")
                                           .Build();

            var connectionString = configuration.GetConnectionString("MySqlConnectionTest");

            var options = new DbContextOptionsBuilder <TodoAppDbContext>()
                          .UseMySql(connectionString).Options;

            using (var context = new TodoAppDbContext(options))
            {
                await context.Database.MigrateAsync().ConfigureAwait(false);

                var dbSet = context.Set <TEntity>();

                var allEntities = await dbSet.ToListAsync().ConfigureAwait(false);

                dbSet.RemoveRange(allEntities);
                //await context.SaveChangesAsync().ConfigureAwait(false);

                await dbSet.AddRangeAsync(entities).ConfigureAwait(false);

                await context.SaveChangesAsync().ConfigureAwait(false);
            }
        }
Пример #3
0
        /// <summary>
        /// Gets all entities
        /// </summary>
        /// <returns></returns>
        public Task <IEnumerable <T> > GetAll()
        {
            using (TodoAppDbContext context = _contextFactory.CreateDbContext())
            {
                IEnumerable <T> entities = context.Set <T>().ToList();

                return((Task <IEnumerable <T> >)entities);
            }
        }
Пример #4
0
        /// <summary>
        /// Gets entity
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task <T> Get(int id)
        {
            using (TodoAppDbContext context = _contextFactory.CreateDbContext())
            {
                T entity = context.Set <T>().FirstOrDefault((e) => e.Id == id);

                return(entity);
            }
        }
Пример #5
0
        /// <summary>
        /// Gets all entities
        /// </summary>
        /// <returns></returns>
        public List <T> GetAllItems()
        {
            using (TodoAppDbContext context = _contextFactory.CreateDbContext())
            {
                List <T> entities = context.Set <T>().ToList();

                return(entities);
            }
        }
Пример #6
0
        /// <summary>
        /// Gets all tasks
        /// </summary>
        /// <returns></returns>
        public async Task <IEnumerable <TaskModel> > GetAll()
        {
            using (TodoAppDbContext context = _contextFactory.CreateDbContext())
            {
                IEnumerable <TaskModel> entities = await context.Set <TaskModel>().ToListAsync();

                return(entities);
            }
        }
Пример #7
0
        /// <summary>
        /// Creates an entity
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task <T> Create(T entity)
        {
            using (TodoAppDbContext context = _contextFactory.CreateDbContext())
            {
                var createdEntity = context.Set <T>().Add(entity);
                context.SaveChanges();

                return(createdEntity);
            }
        }
Пример #8
0
        /// <summary>
        /// Updates entity
        /// </summary>
        /// <param name="id"></param>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task <T> Update(int id, T entity)
        {
            using (TodoAppDbContext context = _contextFactory.CreateDbContext())
            {
                entity.Id = id;

                context.Set <T>().Add(entity);

                await context.SaveChangesAsync();

                return(entity);
            }
        }
Пример #9
0
 public async Task <List <TEntity> > GetEntitiesAsync(Expression <Func <TEntity, bool> > include = null)
 => await _todoAppDbContext.Set <TEntity>().Where(include).ToListAsync().ConfigureAwait(false);