.NET Tips and Tricks

Blog archive

Updating Entities from DTOs with AutoMapper

About a year ago, Patrick Steele did a great job of describing AutoMapper: an absolutely necessary tool if you're using Data Transfer Objects to move data between parts of your application. AutoMapper will automatically move data from your business entities into the corresponding properties on your DTO, saving you many, many lines of repetitious assignment statements. It also means that if you add a new property to your DTO, AutoMapper will automatically pick up the corresponding property from the entity; no further changes to your code required.

You do need to be careful, however, when moving your data from a DTO and into an Entity Framework (EF) entity object. By default, AutoMapper creates a new instance of the object that the data is being moved into. If you write code like the following, for instance, the Customer object in custExisting that's returned from AutoMapper is a different object from the Customer object retrieved from the DbContext (even though the custExisting variable points to both of those objects):

Mapper.CreateMap(Of CustomerDTO, Customer)()
custExisting = dbs.Customers.Single(Function(c) c.Id = custDTO.Id)
custExisting = Mapper.Map(Of CustomerDTO, Customer)(custDTO)

That's bad, because EF won't track the changes made to that new custExisting object: When you call SaveChanges, nothing will be saved back to the database. This is a common-enough problem that there's even some misinformation about it floating around. I've seen a couple of forums, for instance, where people with this problem have been told that AutoMapper can't be used to update entities.

You can update entities with AutoMapper. Here's how: pass both the DTO and the entity object to AutoMapper's Map method. That's what this code does:

custExisting = Mapper.Map(Of CustomerDTO,  Customer)(custDTO, custExisting)

Rather than creating a new entity object to return from the Map method, AutoMapper now updates the entity object passed in the second parameter and returns it to my custExisting variable. As a result, EF will track the changes made to the entity object; and, when you call SaveChanges, your updates will be sent to the database.

Posted by Peter Vogel on 11/06/2013


comments powered by Disqus

Featured

  • Full Stack Hands-On Development with .NET

    In the fast-paced realm of modern software development, proficiency across a full stack of technologies is not just beneficial, it's essential. Microsoft has an entire stack of open source development components in its .NET platform (formerly known as .NET Core) that can be used to build an end-to-end set of applications.

  • .NET-Centric Uno Platform Debuts 'Single Project' for 9 Targets

    "We've reduced the complexity of project files and eliminated the need for explicit NuGet package references, separate project libraries, or 'shared' projects."

  • Creating Reactive Applications in .NET

    In modern applications, data is being retrieved in asynchronous, real-time streams, as traditional pull requests where the clients asks for data from the server are becoming a thing of the past.

  • AI for GitHub Collaboration? Maybe Not So Much

    No doubt GitHub Copilot has been a boon for developers, but AI might not be the best tool for collaboration, according to developers weighing in on a recent social media post from the GitHub team.

  • Visual Studio 2022 Getting VS Code 'Command Palette' Equivalent

    As any Visual Studio Code user knows, the editor's command palette is a powerful tool for getting things done quickly, without having to navigate through menus and dialogs. Now, we learn how an equivalent is coming for Microsoft's flagship Visual Studio IDE, invoked by the same familiar Ctrl+Shift+P keyboard shortcut.

Subscribe on YouTube