SQL Server · Update

Update without a catalog

Updates on a service created with only a connection string, in two scenarios: by the id, which ARX reads from the table's primary key; and a table with no primary key, found by columns, which needs no id at all.

Set up for this page

Besides the sample itself, it uses SampleContext.cs (the dbContext every sample is given); SampleOutput.cs (Step and Show, which print), in the files below.

Before scenario 1 the sample runs this, so the service and the rows the scenarios use are there:

Set up
var service = new ArxMsSqlDbService(connectionString);

1 By the id

The usual way: the id Create returned, on an entity with only the attributes to change.

Already in scope: service (ArxMsSqlDbService)

C#
var account = new ArxEntity("account");
account["name"] = "Blue Yonder Airlines";
account["account_number"] = "ACC-N021";
account["employee_count"] = 40;
ArxRecordId id = await service.CreateAsync(account);

var change = new ArxEntity("account");
change.RecordId = id;
change["employee_count"] = 45;

int changed = await service.UpdateAsync(change);

Console.WriteLine($"account {id.AsInt64()}: {changed} row changed");

Check it in the database

SQL

Run it in any SQL tool connected to the database, after the code has run.

SELECT name, employee_count
FROM account
WHERE account_number = 'ACC-N021';

Expect: 1 row, employee_count 45.

2 A table with no primary key, by columns

activity_log has no primary key: find the rows by a column instead, and every matching row changes.

Already in scope: service (ArxMsSqlDbService)

C#
for (var i = 0; i < 2; i++)
{
    var entry = new ArxEntity("activity_log") { IdColumn = ArxIdColumn.NoIdColumn };
    entry["action"] = "imported the rate card";
    await service.CreateAsync(entry);
}

// "action" finds the rows; logged_at is the value written.
var change = new ArxEntity("activity_log");
change["action"] = "imported the rate card";
change["logged_at"] = new DateTimeOffset(2026, 3, 1, 9, 0, 0, TimeSpan.Zero);

int changed = await service.UpdateAsync(change, new ArxUpdateOptions { Match = ArxMatch.Columns("action") });

Console.WriteLine($"{changed} log entries changed");

Check it in the database

SQL

Run it in any SQL tool connected to the database, after the code has run.

SELECT action, logged_at
FROM activity_log
WHERE action = 'imported the rate card';

Expect: 2 rows, both logged_at 2026-03-01 09:00:00.0000000 +00:00.

Good to know

Every call reads its table from the database first, which makes it much slower than a service with a catalog (Create Basic6 says when that is fine).

An update by the id (the default) on a table with no usable primary key is ARX1019 TableNotResolvable. By a key or by columns, the table needs no id, so the update runs.

Run the complete sample

The code above comes from one program, built in a fresh project and run against the database. To run it yourself, here is every file and step.

The program, its files and how to run it

Run it locally

  1. .NET SDK: .NET SDK 10.0.100 or later 10.0.x (target net10.0).
  2. Create the project:
    Terminal
    dotnet new console -n ArxSample -f net10.0
    cd ArxSample
  3. Install the packages:
    Terminal
    dotnet add package ARX.SDK.SQL --version 1.0.0-rc.1
  4. Database: a SQL Server 2022 (tested with 16.0.4225.2) database with the sample schema.
  5. Configuration: set ARX_SAMPLES_SQLSERVER to its connection string. Nothing is hard-coded.
  6. Source code: replace Program.cs and add the 3 other files below, with the file names shown.
  7. Run it:
    Terminal
    dotnet run
  8. Expected output: below.

The files

Program.cs

Runs the sample. Generated for this page; it reads the connection string from the environment.

using ArxSamples.MSSQL;
using ArxSamples.MSSQL.Operations.Update.Basics;

// The connection string comes from the environment, never from the code (see "Configure the connection").
var connectionString = Environment.GetEnvironmentVariable("ARX_SAMPLES_SQLSERVER")
    ?? throw new InvalidOperationException("Set the ARX_SAMPLES_SQLSERVER environment variable to your SQL Server connection string.");

await new Basic5_WithoutACatalog().RunAsync(new SampleContext(connectionString));
Basic5_WithoutACatalog.cs

The sample, as it is in the repository.

using ARX.SDK;
using ARX.SDK.SQL.Services;
using ARX.SDK.Services;

using static ArxSamples.MSSQL.SampleOutput;

namespace ArxSamples.MSSQL.Operations.Update.Basics;

/// <summary>
/// Updates on a service created with only a connection string, in two scenarios: by the id, which ARX reads from the
/// table's primary key; and a table with no primary key, found by columns, which needs no id at all.
/// </summary>
/// <remarks>
/// <para>Every call reads its table from the database first, which makes it much slower than a service with a catalog
/// (Create Basic6 says when that is fine).</para>
/// <para>An update by the id (the default) on a table with no usable primary key is ARX1019 TableNotResolvable. By a
/// key or by columns, the table needs no id, so the update runs.</para>
/// </remarks>
public sealed class Basic5_WithoutACatalog : ISample
{
    public string Name => "update-basics-5-without-a-catalog";

    public string Title => "Update without a catalog";

    public async Task RunAsync(SampleContext dbContext)
    {
        var service = new ArxMsSqlDbService(dbContext.ConnectionString);

        await Scenario1_ByTheIdAsync(service);

        await Scenario2_ATableWithNoPrimaryKey_ByColumnsAsync(service);
    }

    /// <summary>1. The usual way: the id Create returned, on an entity with only the attributes to change.</summary>
    private static async Task Scenario1_ByTheIdAsync(ArxMsSqlDbService service)
    {
        Step("1. By the id");

        var account = new ArxEntity("account");
        account["name"] = "Blue Yonder Airlines";
        account["account_number"] = "ACC-N021";
        account["employee_count"] = 40;
        ArxRecordId id = await service.CreateAsync(account);

        var change = new ArxEntity("account");
        change.RecordId = id;
        change["employee_count"] = 45;

        int changed = await service.UpdateAsync(change);

        Show($"account {id.AsInt64()}: {changed} row changed");

        /* ===========================================================
         * [DIRECT QUERY CHECK]
         * -----------------------------------------------------------
         * SELECT name, employee_count
         * FROM account
         * WHERE account_number = 'ACC-N021';
         * -- Expect: 1 row, employee_count 45.
         * ===========================================================
         */
    }

    /// <summary>2. activity_log has no primary key: find the rows by a column instead, and every matching row changes.</summary>
    private static async Task Scenario2_ATableWithNoPrimaryKey_ByColumnsAsync(ArxMsSqlDbService service)
    {
        Step("2. A table with no primary key, by columns");

        for (var i = 0; i < 2; i++)
        {
            var entry = new ArxEntity("activity_log") { IdColumn = ArxIdColumn.NoIdColumn };
            entry["action"] = "imported the rate card";
            await service.CreateAsync(entry);
        }

        // "action" finds the rows; logged_at is the value written.
        var change = new ArxEntity("activity_log");
        change["action"] = "imported the rate card";
        change["logged_at"] = new DateTimeOffset(2026, 3, 1, 9, 0, 0, TimeSpan.Zero);

        int changed = await service.UpdateAsync(change, new ArxUpdateOptions { Match = ArxMatch.Columns("action") });

        Show($"{changed} log entries changed");

        /* ===========================================================
         * [DIRECT QUERY CHECK]
         * -----------------------------------------------------------
         * SELECT action, logged_at
         * FROM activity_log
         * WHERE action = 'imported the rate card';
         * -- Expect: 2 rows, both logged_at 2026-03-01 09:00:00.0000000 +00:00.
         * ===========================================================
         */
    }
}
SampleContext.cs

What every sample is given: the connection string (and the ISample interface the samples implement).

namespace ArxSamples.MSSQL;

/// <summary>One documentation sample: a scenario a real application has, shown end to end.</summary>
public interface ISample
{
    /// <summary>The name to run it alone: <c>dotnet run -- &lt;name&gt;</c>.</summary>
    string Name { get; }

    /// <summary>One line on what it shows.</summary>
    string Title { get; }

    Task RunAsync(SampleContext dbContext);
}

/// <summary>
/// What a sample is given. Every sample gets the connection string; the application's service and catalog
/// (SampleContext.Application.cs) and the plain SqlClient read-back helper (SampleContext.Direct.cs) live in their own
/// files, so a sample needs only the files it uses.
/// </summary>
public sealed partial class SampleContext
{
    public SampleContext(string connectionString)
    {
        ConnectionString = connectionString;
    }

    public string ConnectionString { get; }
}
SampleOutput.cs

How the samples print: Step for each scenario, Show for what it did, ShowError for an error caught on purpose.

using ARX.SDK;

namespace ArxSamples.MSSQL;

/// <summary>How the samples print: a numbered step for each scenario, then what it did.</summary>
public static class SampleOutput
{
    /// <summary>The start of a scenario: <c>Step("1. By the id Create returned")</c>.</summary>
    public static void Step(string text) => Console.WriteLine($"  > {text}");

    /// <summary>What the scenario did.</summary>
    public static void Show(string text) => Console.WriteLine($"      {text}");

    /// <summary>An ARX error the scenario caught on purpose.</summary>
    public static void ShowError(ArxException exception)
        => Console.WriteLine($"      caught {exception.GetType().Name}: {exception.Message}");
}

Expected output

What dotnet run printed when the sample was verified, on a freshly created sample schema. Ids differ when the tables already hold rows.

Output
  > 1. By the id
      account 1: 1 row changed
  > 2. A table with no primary key, by columns
      2 log entries changed