Exemple #1
0
        /// <summary>
        /// Updates a todo-item in the data context.
        /// </summary>
        /// <param name="item">Item to update.</param>
        /// <returns>Success value.</returns>
        public async Task <bool> UpdateTodo(TodoViewModel item)
        {
            using (var context = new TolldoDbContext())
            {
                // Get item to update
                var itemToUpdate = await context.Todos.Where(i => i.Id == item.Id).FirstOrDefaultAsync();

                _mapper.Map(item, itemToUpdate);

                if (itemToUpdate == null)
                {
                    return(false);
                }

                // Update item in database
                itemToUpdate.Tasks = null;
                context.Update(itemToUpdate);

                // Ignore tasks
                context.Entry(itemToUpdate).Collection(x => x.Tasks).IsModified = false;

                return(await context.SaveChangesAsync() > 0 ? true : false);
            }
        }