Update Record

Syntax

Update(entity);

Arguments

entity

T

Instance of to-be-updated record containing the intended changes.

This parameter must not be empty.

Description

Updates an existing record of a repository by taking an instance of the record with changed values as input and updating the record in the repository accordingly.

It can only update one record at a time but if the same changes should be done for multiple records, you can use loops to automate the process to save time (see Example 2).

Examples

Example 1: Updating a single record

  1. load the record you wish to update into a variable, e.g. by using "GetByID" or "Find"

  2. update the specific attributes you wish to change by accessing those attributes using the dot operator on the variable and assigning them the new values

  3. confirm the update by calling this function "Update" with the variable as input

The following program illustrates this process:

//1.Step: load data
var orderChange = repo.GetByID("EU00789980");

//2.Step: update attributes
orderChange.DueDate = DateTime.Today;
orderChange.IsPlan = true;
orderChange.ChangedBy = "user.name";

//3.Step: confirm update
repo.Update(orderChange);

The function will update the data of the specific record in the repository that has the Primary Key value "EU00789980" according to the changes made in the variable.

Example 2: Updating multiple records

  1. load all the records you wish to update into a variable. e.g. by using "Get"

  2. loop through the loaded records and, within the loop, update the specific attributes you wish to change by accessing those attributes using the dot operator on the loop variable and assigning them the new values

  3. confirm the update by calling this function "Update" within the loop

The following program illustrates this process:

//1.Step: load data
var orderBatch = repo.Get(x => x.ProductID == "ADAM-567-BTO" && x.IsPlan == false);

//2.Step: update attribute
foreach(var item in orderBatch)
{
    item.Quantity = 10;
    item.ChangedDate = DateTime.Now;
    item.ChangedBy = "user.name";

//3.Step: confirm change
    repo.Update(item);
}

At the end of every iteration of the loop, the function will update the data of the currently processed record according to the changes made in the loop variable.

Last updated