コード例 #1
0
        public async Task Delete(int id)
        {
            var entity = await _context.Set <T>().FindAsync(id);

            _context.Set <T>().Remove(entity);
            await _context.SaveChangesAsync();
        }
コード例 #2
0
        protected void UpdateCollection <ModelType, DBModelType>(LibraryDataContext dataContext, ICollection <ModelType> source, ICollection <DBModelType> destination)
            where ModelType : class, Model.IIdRecord
            where DBModelType : class, DB.IIdRecord
        {
            //removed items
            foreach (var oldItem in destination.ToList())
            {
                if (!source.Any(x => x.Id == oldItem.Id))
                {
                    destination.Remove(oldItem);
                }
            }

            //added items
            foreach (var newItem in source)
            {
                if (!destination.Any(x => x.Id == newItem.Id))
                {
                    var toAdd = dataContext.Set <DBModelType>().FirstOrDefault(x => x.Id == newItem.Id);
                    if (toAdd != null)
                    {
                        destination.Add(toAdd);
                    }
                }
            }
        }
コード例 #3
0
 protected void MapCollection <ModelType, DBModelType>(LibraryDataContext dataContext, ICollection <ModelType> source, ICollection <DBModelType> destination)
     where DBModelType : class, DB.IIdRecord
     where ModelType : class, Model.IIdRecord
 {
     destination.Clear();
     foreach (var item in source)
     {
         destination.Add(dataContext.Set <DBModelType>().FirstOrDefault(x => x.Id == item.Id));
     }
 }