简体   繁体   中英

Automapper Missing Map Configuration

I have an Attendee Class and An AttendeeViewModel

The datetime field on the Attendee Model gets set to the default .NET Datetime when i map it from AttendeeViewModel instead of the value that is already existing in the Attendee Model

Here's my AttendeeViewModel

public class AttendeeViewModel
{

    public int Id { get; set; }
    [Required]
    [EmailAddress]
    public string Email { get; set; }
    [Required]
    [Display(Name = "First Name")]
    public string FirstName { get; set; }
    [Required]
    [Display(Name = "Last Name")]
    public string LastName { get; set; }
    public int FEventId { get; set; }

    public string DisplayName
    {
        get { return string.Format("{0}, {1}", FirstName, LastName); }
    }
}

Here's my Base AttendeeModel

public class Attendee
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public  int Id { get; set; }
    [Required]
    public  string Email { get; set; }
    [Required]
    public  string FirstName { get; set; }
    [Required]
    public  string LastName { get; set; }
    public DateTime CreatedAt { get; set; }

    public bool IsActive { get; set; }


    public int FEventId { get; set; }

    public virtual ApplicationUser CreatedBy { get; set; }

    public virtual FEvent FEvent { get; set; }

    public ICollection<ProjectPledge> ProjectPledges { get; set; } 

}

Here's My mapping configuration

public static void Configure()
    {

       Mapper.CreateMap<AttendeeViewModel, Attendee>().ForMember(dest=>dest.CreatedAt , opt=>opt.Ignore());

    } 

And heres's the Controller Action

[HttpPost]
    [ValidateAntiForgeryToken]
    public virtual ActionResult Edit(AttendeeViewModel attendee)
    {
        if (!_attendeeService.CanAddAttendee(attendee.Email, attendee.FilanthropyEventId))
        {
            AddEmailModelError();
        }

        if (ModelState.IsValid)
        {
            var mappedAttendee = _attendeeService.GetById(attendee.Id);

            mappedAttendee = Mapper.Map<AttendeeViewModel, Attendee>(attendee);
            _attendeeService.AddOrUpdate(mappedAttendee);
            return RedirectToAction(MVC.Attendee.Index(mappedAttendee.FilanthropyEventId));
        }
        return View(attendee);
    }

if I set the configuration to be this insetad of opt.Ignore()

Mapper.CreateMap<AttendeeViewModel, Attendee>().ForMember(dest=>dest.CreatedAt , opt=>opt.UseDestinationValue());

The Mapping fails giving this exception

Missing type map configuration or unsupported mapping.

Mapping types:
AttendeeViewModel -> DateTime
MyProject.Web.ViewModels.AttendeeViewModel -> System.DateTime

Destination path:
Attendee.CreatedAt.CreatedAt

Source value:
MyProject.Web.ViewModels.AttendeeViewModel

Any ideas on how i can resolve this?

If you want to map onto an existing object you need to use the overload that takes the existing destination:

Mapper.Map<Source, Destination>(source, destination);

that should do the trick.

Have you tried removing the ".ForMember" section and just let AutoMapper ignore it? In order to help you any more it would be helpful to see the two models for comparison.

Update: after lookind at your models I would suggest the following should solve the issue you are having...

Mapper.CreateMap <attendeeviewmodel, attendee>.ForMember (x => x.CreatedAt, opt => opt.MapFrom (src => datetime.utcnow));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM