Dela via


DeleteRequest.ConcurrencyBehavior Property

Definition

Specifies the type of optimistic concurrency behavior that should be performed by Dataverse when processing this request.

public:
 property Microsoft::Xrm::Sdk::ConcurrencyBehavior ConcurrencyBehavior { Microsoft::Xrm::Sdk::ConcurrencyBehavior get(); void set(Microsoft::Xrm::Sdk::ConcurrencyBehavior value); };
public Microsoft.Xrm.Sdk.ConcurrencyBehavior ConcurrencyBehavior { get; set; }
member this.ConcurrencyBehavior : Microsoft.Xrm.Sdk.ConcurrencyBehavior with get, set
Public Property ConcurrencyBehavior As ConcurrencyBehavior

Property Value

The concurrency behavior.

Examples

The following example shows setting the concurrency behavior to use ConcurrencyBehavior.IfRowVersionMatches with a record from the account table, which has IsOptimisticConcurrencyEnabled set to true.

/// <summary>
/// Demonstrates use of optimistic concurrency on delete.
/// </summary>
/// <param name="service">Authenticated IOrganizationService instance</param>
static void DeleteRecordOptimisticConcurrency(IOrganizationService service) {

    Guid accountId = new("93b3e71c-9a51-ee11-be6f-000d3a993550");
    string rowVersion = "95046280";

    // Simulate an account record retrieved
    Entity retrievedAccount = new("account") { 
        Id = accountId,
        RowVersion = rowVersion
    };

    DeleteRequest request = new() { 
          Target = retrievedAccount.ToEntityReference(),
          ConcurrencyBehavior = ConcurrencyBehavior.IfRowVersionMatches
    };

    try
    {
        service.Execute(request);
    }
    catch (FaultException<OrganizationServiceFault> ex)
    {
        switch (ex.Detail.ErrorCode)
        {
            case -2147088243: // ConcurrencyVersionNotProvided
            case -2147088253: // OptimisticConcurrencyNotEnabled 
            case -2147088254: // ConcurrencyVersionMismatch                                        
                Console.WriteLine($"Code: {ex.Detail.ErrorCode} Message: {ex.Detail.Message}");
                break;
            default:
                throw ex;
        }
    }
}

Output

If the rowVersion doesn't match the current version on the server:

Code: -2147088254 Message: The version of the existing record doesn't match the RowVersion property provided.

If the EntityReference.RowVersion property set to the DeleteRequest.Target property doesn't contain a value:

Code: -2147088253 Message: The RowVersion property must be provided when the value of ConcurrencyBehavior is IfVersionMatches.

Remarks

Dataverse supports multiple concurrent users and applications. Someone using your application may retrieve a record and, depending on the data in the record, decide to delete it.

In the time between your application retrieving the record and the user deciding to delete it, another user might update the record in a way that the person using your application might decide not to delete it. Use this property to enable a check to see if the record to be deleted was changed since it was last retrieved by your application. If it has been changed, Dataverse will return an error. Your application can tell the user that the record was changed since it was retrieved last and provide another opportunity to review and possibly delete the record.

This capability is only enabled for tables where the EntityMetadata.IsOptimisticConcurrencyEnabled property is true. It is true for most tables in Dataverse, but you can check this by examining the table definition for the specific table. Learn to view detailed table definition data in your environment

Applies to