Esempio n. 1
0
        public async Task UpdateAsync(TEntity entity)
        {
            var info = await _dbContext.FindAsync <TEntity>(entity.Id);

            if (info != null)
            {
                //database  property
                var types    = info.GetType();
                var property = types.GetProperties();
                //entity property
                var entityType         = entity.GetType();
                var entityTypeProperty = entityType.GetProperties();
                foreach (var item in property)
                {
                    switch (item.Name)
                    {
                    case nameof(info.CreateTime):
                        continue;

                    case nameof(info.DeleteTime):
                        continue;

                    case nameof(info.IsDelete):
                        continue;

                    case nameof(info.Id):
                        continue;

                    case nameof(info.ModifyTime):
                        item.SetValue(info, DateTime.Now);
                        continue;

                    default:
                        break;
                    }
                    foreach (var entityItem in entityTypeProperty.Where(w => w.GetCustomAttribute <NotMappedAttribute>() == null))
                    {
                        if (entityItem.Name == item.Name)
                        {
                            item.SetValue(info, entityItem.GetValue(entity));
                        }
                    }
                }
                await SaveAsync();
            }
        }
        public override async Task <TEntityDto> LoadAsync(TPrimaryKey id)
        {
            var entity = await _dbContext.FindAsync <TEntity>(id);

            return(Mapper.Map <TEntityDto>(entity));
        }