-
Notifications
You must be signed in to change notification settings - Fork 96
Update
Jon P Smith edited this page Apr 10, 2018
·
13 revisions
The update of an entity in the database uses CrudServices's UpdateAndSave
method.
In web applications this has a typical two-stage update consisting of
-
ReadSingle<EntityOrDto>(key)
to show the user the current state, some of which will be editable. -
UpdateAndSave(Data)
to update the entity.
The RazorAppPage application contains multiple examples of the use of the UpdateAndSave
method:
- Pages/Home/ChangePubDate - updates book's PublishedOn property.
- Pages/Home/AddReview - adds a new review to the book.
- Pages/Home/AddPromotion - adds/updates a price promotion on a book.
- Pages/Home/RemovePromotion - removes a price promotion on a book (See note 4).
-
Pages/Authors/Edit - updates an
Author
entity using the AutoMapper Save mapping.
- If the entity class has any methods that return
void
orIStatusGeneric
then it will look at these to do the update. Otherwise it tries to use AutoMapper. - If there are methods in the entity it will try to match the DTOs name, (minus a set of possible DTO endings) with a method. If there is a match it will set this as the default one to use.
- You can state exactly what type/name of method/AutoMapper you want to use, by providing a second parameter to the command, e.g.
_service.UpdateAndSave(Data, "RemovePromotion")
. This is useful if there are multiple methods that will match the DTO non-read-only properties. The options for the second parameter are:- methodName - use a specific named static method, e.g. "AddPromotion"
- methodName(n) - use a specific named static method with n parameters, e.g. "AddPromotion(3)"
- AutoMapper - use AutoMapper's save mapping to copy the DTO into the entity
- For methods with no parameters, e.g. "RemovePromotion", you must define the method name either in the second parameter of the call, or by providing a PerDtoConfig to the DTO with the
UpdateMethod
overriden with the name.