1

I've got the following models:

public abstract class PersonBase : EntityWithTypedId
    {
        public PersonBase()
        {
            this.ProfileImages = new List<ProfileImage>();
        }

        public string name;

        [Required]
        public string Name
        {
            get { return name; } 
            set
            {
                name = value;
                this.UrlFriendlyName = value.ToUrlFriendly();
            }
        }

        public string UrlFriendlyName { get; protected set; }

        [UIHint("UploadImage")]
        public List<ProfileImage> ProfileImages { get; set; }
    }



public class ProfileImage
        {
            public int PersonId { get; set; }

            public byte[] Image { get; set; }        
        }

And my viewmodel:

public class PersonDetailsViewModel
    {
        public string Name { get; set; }

        public IEnumerable<HttpPostedFilebase> ProfileImages { get; set; }
    }

Now my question is, how can I map those with automapper? I mean the ProfileImage also needs the PersonId (which could be inserted by the Entity Framework on insert). Do I need to change the naming in the ViewModel or?

tereško
  • 58,060
  • 25
  • 98
  • 150
Julian
  • 1,105
  • 2
  • 26
  • 57

1 Answers1

2

I'm going to presume that personId is part of your edit route values.

[HttpPost]
public ActionResult Edit(int personId, PersonDetailsViewModel viewModel) {
    var entity = dbContext.//load entity by PersonId;
    AutoMapper.Map(viewModel, entity);
    dbContext.SaveChanges();
}

on startup somewhere

AutoMapper.Create<PersonDetailsViewModel, PersonBase>();
AutoMapper.Create<HttpPostedFilebase, ProfileImage>()
    .ForMember(d => d.PersonId, opt => opt.Ignore())
    .ForMember(d => d.Image, opt => opt.MapFrom(s => {
        MemoryStream target = new MemoryStream();
        model.File.InputStream.CopyTo(target);
        return target.ToArray();
    });

If your Person/Profile Image relationship is setup correctly in EF and you have change tracking turned on (should be on by default) EF should automatically figure out the PersonId for the image.

For an insert it should also just work, with EF doing the heavy lifting around the personId, you may need to create the person entity yourself and add it to the context first, but not neccesarily.

public ActionResult Add(PersonDetailsViewModel viewModel) {
    var entity = new PersonBase();
    dbContext.Add(entity);
    AutoMapper.Map(viewModel, entity);
    dbContext.SaveChanges();
}

It gets a bit trickier when trying to update/delete existing profile images rather than just adding to them each time, I have another stackoverflow answer goes into that in more detail - When using DTOs, Automapper & Nhibernate reflecting changes in child collections of DTO in domain object being updated

Community
  • 1
  • 1
Betty
  • 9,109
  • 2
  • 34
  • 48
  • You could go one small step further and add the conversion from InputStream to byte array logic into an extension method. Here's the code if you're using .NET 4 or greater - http://stackoverflow.com/a/7073124/335545 – Bern Jul 17 '13 at 13:59