Я показываю вам свой код. Я был бы признателен, если бы вы помогли мне или, если возможно, предоставили пример операции CURD с Automapper, где таблицы представляют собой поля изображений. Спасибо.
Я использовал это сопоставление для добавления, и проблем не возникло, но у меня возникла ошибка сопоставления в позиции редактирования.
Код: Выделить всё
public class UserProfilesUpdateDto : BaseEntity
Код: Выделить всё
public string ShortDescription { get; set; }
public IFormFile UserAvatar { get; set; }
public string InstAccount { get; set; }
public string TelegramAccount { get; set; }
public string GooglePlusAccount { get; set; }
public string FaceBookAccount { get; set; }
public string Email { get; set; }
public IFormFile Resume { get; set; }
public int UserId { get; set; }
public string UserAvatarName { get; set; }
public string ResumeName { get; set; }
Код: Выделить всё
public class UserProfilesDto:BaseEntity
{
public string ShortDescription { get; set; }
public IFormFile UserAvatar { get; set; }
public bool IsActive { get; set; }
public string ActiveCode { get; set; }
public string InstAccount { get; set; }
public string TelegramAccount { get; set; }
public string GooglePlusAccount { get; set; }
public string FaceBookAccount { get; set; }
public string Email { get; set; }
public IFormFile Resume { get; set; }
public int UserId { get; set; }
}
CreateMap()
.ReverseMap()
.ForMember(dest => dest.User, act => act.Ignore())
.ForMember(dest => dest.UserId, act => act.MapFrom(x=>x.UserId))
.ForMember(dest => dest.UserAvatar, opt => opt.MapFrom(src => new ImageUpload(_webHostEnvironment).SaveImageFile(src.UserAvatar)))
.ForMember(dest => dest.Resume, opt => opt.MapFrom(src => new ImageUpload(_webHostEnvironment).SaveResumeFile(src.Resume)));
{
частный IWebHostEnvironment только для чтения? _webHostEnvironment;
public ImageUpload(IWebHostEnvironment? webHostEnvironment)
{
_webHostEnvironment = webHostEnvironment;
Код: Выделить всё
public string SaveImageFile(IFormFile formFile)
{
var webRootPath = _webHostEnvironment.WebRootPath;
var uploadPath = Path.Combine(webRootPath, "SiteFiles");
if (!Directory.Exists(uploadPath))
{
Directory.CreateDirectory(uploadPath);
}
var fileName = Guid.NewGuid() +
Path.GetExtension(formFile.FileName);
var filePath = Path.Combine(uploadPath, fileName);
using (var fileStream = new FileStream(filePath, FileMode.Create))
{
formFile.CopyTo(fileStream);
}
return Path.Combine(fileName);
}
Код: Выделить всё
public void UpdateUserProfileInAdmin(UserProfilesUpdateDto editProfile)
{
var profile = GetUserProfileById(editProfile.Id);
profile.UserId = editProfile.UserId;
profile.Resume = editProfile.ResumeName;
profile.UserAvatar = editProfile.UserAvatarName;
profile.Email = editProfile.Email;
profile.FaceBookAccount = editProfile.FaceBookAccount;
profile.GooglePlusAccount = editProfile.GooglePlusAccount;
profile.InstAccount = editProfile.InstAccount;
profile.ShortDescription = editProfile.ShortDescription;
profile.TelegramAccount = editProfile.TelegramAccount;
if (editProfile.UserAvatar != null)
{
if (profile.UserAvatar !=null)
{
var deleteImagePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/SiteFiles",
editProfile.UserAvatarName);
if (File.Exists(deleteImagePath))
{
File.Delete(deleteImagePath);
}
}
}
profile.UserAvatar =
Guid.NewGuid() + Path.GetExtension(editProfile.UserAvatar.FileName);
var imagePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/SiteFiles",
editProfile.UserAvatarName);
using (var stream = new FileStream(imagePath, FileMode.Create))
{
editProfile.UserAvatar.CopyTo(stream);
}
if (editProfile.Resume != null)
{
var deleteImagePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/SiteFiles",
editProfile.ResumeName);
if (File.Exists(deleteImagePath))
{
File.Delete(deleteImagePath);
}
}
profile.Resume =
Guid.NewGuid() + Path.GetExtension(editProfile.Resume.FileName);
var resumePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/SiteFiles",
editProfile.ResumeName);
using (var stream = new FileStream(resumePath, FileMode.Create))
{
editProfile.Resume.CopyTo(stream);
}
// var model = _mapper.Map(profile);
_context.UserProfiles.Update(profile);
_context.SaveChanges();
}
Подробнее здесь: https://stackoverflow.com/questions/789 ... automapper