1

I am trying to update an entity using Entity Framework and save it to the database. When the update is called, my service method retrieves the DTO, assigns its the values of the entity object that the UI passed to it, and then saves it to the database. Instead of manually assigning those values, i'd like to use Automapper, however when I do this, the values that I am not mapping are updated to null. Is there a way in Entity Framework or Automapper to prevent this?

Service method finds the existing object in the database, assigns the new entity's properties to it, then saves:

public void Update(MyEntity updatedEntity, int id)
{
    var existingObject = db.tblmyentity.Find(id);

    existingObject.name = updatedEntity.name;
    existingObject.address = updatedEntity.address;
    existingObject.phone = updatedEntity.phone;

    db.SaveChanges();
}

However, there are values stored in fields of this object not accessible by the UI, such as who modified the object and when. Using AutoMapper to simplify this code (shown below) causes these fields to update to null:

public void Update(MyEntity updatedEntity, int id)
{
    var existingObject = db.tblmyentity.Find(id);

    Mapper.Map(updatedEntity, existingObject);

    db.SaveChanges();
}
7
  • Take a look at this answer stackoverflow.com/questions/4987872/… Commented May 18, 2018 at 14:14
  • Possible duplicate of Ignore mapping one property with Automapper Commented May 18, 2018 at 14:15
  • 1
    Use a DTO only containing the properties to be modified. Or copy the values manually. I wouldn't use AM to implement business rules, or else business rules are scattered all over the place. Commented May 18, 2018 at 14:20
  • @Bit the problem is that they aren't the same properties needed to be ignored every time. This object has different properties updated on different UI pages. It would be nice to say "if the property that is going to be mapped is null, leave it alone and don't change it" Commented May 18, 2018 at 14:33
  • @Gert i'm assuming this is what you mean by business logic, which does suck to use scattered across at such a low level Commented May 18, 2018 at 14:33

1 Answer 1

1

A good practice is to create a (service, api) model that contains only the relevant properties that can be updated. E.g.:

public class MyEntityServiceModel
{
    public string name { get; set; }
    public string address { get; set; }
    public string phone { get; set; }
}

// this looks differently in recent versions of AutoMapper, but you get the idea
Mapper.CreateMap<MyEntityServiceModel, MyEntity>();

// your update functions looks the same, except that it receives a service model, not a data model

Update(MyEntityServiceModel updatedEntity, int id) 
{
   // same code here
}

This approach has the following advantages:

  • you obtain what you are asking for
  • safety: you do not risk updating more properties than you should since the service model clearly specify the properties that should be updated
  • serialization: the service model is more appropriate if you need serialization (EF models may include unwanted navigation properties)
  • Update function consumer becomes unaware of the data persistence library you are using.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.