Exemplo n.º 1
0
        public async Task <Guid> AddAsync(TEntity entity)
        {
            var entry = await _todoAppDbContext.AddAsync(entity).ConfigureAwait(false);

            await _todoAppDbContext.SaveChangesAsync().ConfigureAwait(false);

            return((Guid)entry.Property("Id").CurrentValue);
        }
Exemplo n.º 2
0
        public async void AddTaskAsync(string description, bool done)
        {
            var task = new TaskToDo()
            {
                Description = description, Done = done
            };

            dbContext.Tasks.Add(task);
            await dbContext.SaveChangesAsync();
        }
Exemplo n.º 3
0
        public async Task AddAsync(string description)
        {
            var item = new TodoItem()
            {
                Description = description,
                CreatedDate = DateTime.UtcNow
            };

            dbContext.Add(item);
            await dbContext.SaveChangesAsync();
        }
Exemplo n.º 4
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);
            }
        }
        public async static Task EnsureSeedData(this TodoAppDbContext dbContext)
        {
            if (dbContext.TodoItems.Any())
            {
                return;
            }

            await dbContext.AddRangeAsync(_todoItems);

            await dbContext.SaveChangesAsync();
        }
Exemplo n.º 6
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);
            }
        }
Exemplo n.º 7
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);
            }
        }
Exemplo n.º 8
0
        /// <summary>
        /// Updates task
        /// </summary>
        /// <param name="id"></param>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task <TaskModel> Update(int id, TaskModel entity)
        {
            using (TodoAppDbContext context = _contextFactory.CreateDbContext())
            {
                var editedEntity = context.Tasks.Where(e => e.Id == entity.Id).First();

                context.Entry(editedEntity).CurrentValues.SetValues(entity);

                await context.SaveChangesAsync();

                return(entity);
            }
        }
Exemplo n.º 9
0
        public async Task Add(SubCategory entity)
        {
            await _context.AddAsync(entity);

            await _context.SaveChangesAsync();
        }
Exemplo n.º 10
0
 private Task SaveChanges()
 {
     return(_dbContext.SaveChangesAsync());
 }
 public async Task <bool> SaveChangesAsync()
 {
     return(await _context.SaveChangesAsync() > 0);
 }