1

I have this entity:

public class MyType
{
    public Guid Id {get;set;}
    public string Name {get;set;}
}

I want to merge two of these types together, but the source Id needs to be unchanged.

So the objects are filled as such:

From the Database => MyType {
    Id: 012312-42134-12321-12,
    Name: "This Type"
}

From the View => MySecondType {
    Id: null,
    Name: "This Type Changed"
}

And I want to merge MySecondType into MyType but ignore the Id field in the merge.

So I thought maybe doing:

Mapper.Map(mySecondType, myType);

To get the database myType to be filled with the view mySecondType but there is not setting to ignore a property.

Callum Linington
  • 14,213
  • 12
  • 75
  • 154

1 Answers1

3

When setting up your AutoMapper you could ignore a property:

Mapper
    .CreateMap<MySecondType, MyType>()
    .ForMember(x => x.Id, opt => opt.Ignore());

This being said, it would make far more sense to use a view model in ASP.NET MVC. So you would have your DB bound model MyType with Id and Name properties and have a MyTypeViewModel which will only have a Name property and be used in your view. Then you don't need to be ignoring any properties. This will automatically come from the design of your view models.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • But this doesnt work when trying to edit an entity! I need the `Id` for lookup! I have created that mapping, will that work then when I use `map(object src, object dest)`?? – Callum Linington Jul 26 '14 at 17:41
  • 1
    It's obvious that you should have an EditViewModel and CreateViewModel. One with Id and one without. That's the whole purpose of view models: have them in every page/view. If you think to have a single class being reused in all your views then you will get into lots of trouble and problems like the one you are having at the moment. – Darin Dimitrov Jul 26 '14 at 17:42
  • I have a single page, that is made up of partials! I understand what you are saying, but I do not want to do it that way. I would like to do it the way I have described – Callum Linington Jul 26 '14 at 18:02
  • 1
    Alright, you can do it as you want. I only suggested what would be the correct approach (in addition to providing an answer to your approach using the `opt.Ignore()` when defining the mapping rules). The reason why I insisted on the view models approach is in case someone else stumbles upon this question. – Darin Dimitrov Jul 26 '14 at 18:06
  • So, do you use the `map(object src, object dest)` when you set that configuration? – Callum Linington Jul 26 '14 at 18:45
  • You use `Map(object src, object dest)` when you want to perform the actual mapping between two type instances. You use `CreateMap` when you are configuring the mapping between those types, usually in your `Application_Start`. That's where you can customize the actual mapping and indicate that some property can be ignored. All this is described in the documentation of `AutoMapper`. – Darin Dimitrov Jul 26 '14 at 21:04
  • @DarinDimitrov, re: your recommendation to have separate EditViewModel and CreateViewModel - how do you recommend solving the original problem in the EditViewModel? I.e., how would you guarantee that EditViewModel, which does contain the ID, does not overwrite the ID when updating, due to over-posting? – JKubecki Dec 21 '16 at 19:58