Answer the question
In order to leave comments, you need to log in
How to act in the following situation?
Good afternoon. I will briefly describe what the problem is.
Application on Asp.Net MVC 4 + EF. Let's say there is a domain model
class Model
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public string UserName { get; set; }
public string Email { get; set; }
public string PhoneNumber { get; set; }
}
Answer the question
In order to leave comments, you need to log in
in AutoMapper you can set mapping rules from model to viewmodel and vice versa.
the first one that came across on habré:
habrahabr.ru/post/71820
custom configuration as the author puts it ...
but in general, when I didn’t know about the existence of AutoMapper, I manually moved each field from ViewModel to DbModel,
and I’m sure in most cases I’ll have to do it manually overtake, AutoMapper will not always be able to help out ... although I may be wrong, since I am not an avid fan of the latter.
I figured it out myself. In vain probably created a question. AutoMapper has an overloaded method that maps a source object to an existing object. Those. all fields from the ViewModel will be displayed on the Model, and the "extra" fields of the Model will remain unchanged.
//...
Model fromDb = GetModelFromDbById(id);
Model model = Mapper.Map<ViewModel, Model>(viewModel, fromDb);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question