‫۲ سال قبل، شنبه ۲۲ مرداد ۱۴۰۱، ساعت ۱۳:۰۲
نکته تکمیلی
در صورتیکه به زمانِ الان، برای پارامتر هاتون نیاز داشتید اونو از طریق سرویس پاس بدید و متغیر رو داخل Profile به این صورت تعریف نکنید:
public class MappingProfile : Profile
{
    var now = DateTime.Now;
}
چون همون بار اول که پروژه اجرا میشه، مقدار دهی میشه و زمانش تغییر پیدا نمیکنه، DateTime.Now باید هربار از طریق سرویس از نو مقداردهی بشه.
public class MappingProfile : Profile
{
    DateTime now = default;
}
return _products
            .AsNoTracking()
            .ProjectTo<ProductInfoViewModel>(
                configuration: _mapper.ConfigurationProvider,
                parameters: new { now = DateTime.Now }
            ).SingleOrDefaultAsync(x => x.Id == id);

‫۲ سال و ۱ ماه قبل، شنبه ۲۵ تیر ۱۴۰۱، ساعت ۲۲:۲۴
نکته تکمیلی
جهت گرفتن تعداد رکورد‌های یک Navigation Property هنگام استفاده از EF میتوان از قوانین توکار خود Automapper استفاده کرد و نیازی به نوشتن ForMember وجود ندارد.
public class ProductInfoViewModel
{
    // long
    public long ProductCommentsLongCount { get; set; }

    // int
    public int ProductCommentsCount { get; set; }
}
اسم Navigation property + اسم متود، اگر کلید اصلی جدول کامنت int  باشه از ProductCommentsCount و اگر هم long باشه از ProductCommentsLongCount استفاده میکنیم. 
public class Product
{
    #region Properties

    public long Id { get; set; }

    [Required]
    [MaxLength(200)]
    public string Title { get; set; }

    public int Price { get; set; }

    #endregion

    #region Relations

    public ICollection<ProductComment> ProductComments { get; set; }

    #endregion
}

public class ProductComment
{
    #region Properties

    public int Id { get; set; }

    public string CommentBody { get; set; }

    public long ProductId { get; set; }

    #endregion

    #region Relations

    public Product Product { get; set; }

    #endregion
}

public class MappingProfile : Profile
{
    public MappingProfile()
    {
        this.CreateMap<Entities.Product, ProductInfoViewModel>();
    }
}
دیگه نیازی به نوشتن ForMember برای گرفتن Count وجود نداره.
‫۵ سال و ۱۱ ماه قبل، چهارشنبه ۴ مهر ۱۳۹۷، ساعت ۱۸:۳۵
سلام؛ ما در پروژه هامون معمولا کار Object Mapping رو توی لایه Repository انجام نمیدیم و در لایه Service کار تبدیل Model به ViewModel رو انجام میدیم ..  
var jobViewModel = Mapper.Map<Job>(jobViewModel);
به نظرتون استفاده مستقیم از متد ProjectTo در AutoMapper در لایه ای مثل Repository کار درستیه ؟ 
روشی دیگر جهت انتقال Data Annotations از Model به ViewModel

در ASP MVC  با MetadataTypeAttribute .
 روش کار به این صورت است که کلاس ViewModel را به Attribute (MetadataType ) مزین می‌کنیم که این Attribute  در سازنده خود تایپ Model را دریافت می‌کند و همچنین  در فضای نامی زیر قرار دارد 
System.ComponentModel.DataAnnotations 
    public class Student
    {
        public int Id { get; set; }

        [Required(ErrorMessage = "نام ضروری است")]
        [Display(Name = "نام")]
        public string Name { get; set; }
    }

ViewModel
    [MetadataType(typeof(Student))]
    public class StudentViewModel
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }

در ASP MVC Core  هم روش کار به همین صورت است بجای MetadataType از ModelMetadataType  استفاده کنید .
‫۷ سال و ۸ ماه قبل، یکشنبه ۱۰ بهمن ۱۳۹۵، ساعت ۰۱:۰۱
با سلام ، من یه مشکلی توی projection  دارم ، به اینصورت که مثل کد زیر انجام دادم ولی value و text را null بر میگردونه
.ProjectTo<SelectListItem>(parameters:null,configuration:_mapper.ConfigurationProvider)
                   .ToListAsync();
و در قسمت پروفایل هم به شکل زیر
            CreateMap<Department, SelectListItem>()
                .ForMember(x => x.Value, m => m.MapFrom(s => s.Id.ToString()))
                .ForMember(x => x.Text, m => m.MapFrom(s => s.Name));
‫۷ سال و ۸ ماه قبل، چهارشنبه ۲۲ دی ۱۳۹۵، ساعت ۱۹:۳۳
نسخه 5.2.0 Automapper 
چگونه از member هایی که map نشده اند چشم پوشی کنیم؟
به عنوان مثال در LoginViewModel  تنها نیاز است نام کاربری و رمز عبور را دریافت کنیم اما در مدل اصلی یعنی User فیلد‌های دیگر هم وجود دارد.
برای این کار از کد زیر استفاده کردم اما باز هم با استثنا رخ می‌دهد.
   public LoginProfile()
   {
      CreateMap<LoginViewModel, User>().ForAllMembers(_ => _.Ignore());
      CreateMap<LoginViewModel, User>().ForMember(_ => _.UserName , __ => __.MapFrom(_ => _.UserName));
      CreateMap<LoginViewModel, User>().ForMember(_ => _.PasswordHash , __ => __.MapFrom(_ => _.Password));
   }

استثنا :
Unmapped members were found. Review the types and members below.
Add a custom mapping expression, ignore, add a custom resolver, or modify the source/destination type
For no matching constructor, add a no-arg ctor, add optional arguments, or map all of the constructor parameters
==========================================================================================
LoginViewModel -> User (Destination member list)
App.ViewModel.Enities.Identity.LoginViewModel -> App.DomainClasses.Entities.Identity.User (Destination member list)

Unmapped properties:
FirstName
LastName
IsSystemAccount
IsBan
RegisterDate
LastLoginDate
RowVersion
City
CityId
 // more ...