Skip to content

DoNotCopyBackToDatabase

Jon Smith edited this page Apr 20, 2015 · 1 revision

The [DoNotCopyBackToDatabase] attribute can be applied to properties in a DTO that is used in a Create or Update. Its effect is to stop AutoMapper copying that property into the EF data class when a Create or Update is being executed. (Note: the name is slightly misleading - it should really be called [DoNotCopyBackToDataEntityClass], but the name was already too long!).

[DoNotCopyBackToDatabase] is GenericServices's version of MVC's [Bind(Exclude = "SomeProperty")] . Both allow a property to be marked as not being overwritten by user input.

This is useful for properties that you want to show to a user, but you don't want them to update, e.g. you want a user to see how much an item in their order costs, but if they update the order they cannot change the cost.

In the two examples I also use the attribute as a comment, i.e. it shows me that the property is not updated by the DTO but is actually updated by EF or inside SaveChanged().