MySQL · Delete
Remove a contact by id or by email
Removing contacts, in two scenarios: from the contact screen (by id), and an "unsubscribe and forget me" request that only gives the email address (the declared key).
Set up for this page
The scenarios build on the sample application: SampleContext.cs (the dbContext every sample is given); SampleOutput.cs (Step and Show, which print); SampleContext.Application.cs (dbContext.Service, the application's ARX service); SampleCatalog.cs (the tables' ids, keys, lookups and display names).
Before scenario 1 the sample runs this, so the service and the rows the scenarios use are there:
var first = new ArxEntity("contact");
first["first_name"] = "Rosa";
first["last_name"] = "Delgado";
first["email"] = "rosa.delgado@example.com";
var second = new ArxEntity("contact");
second["first_name"] = "Omar";
second["last_name"] = "Haddad";
second["email"] = "omar.haddad@example.com";
var ids = await service.CreateMultipleAsync(new List<ArxEntity> { first, second });1 From the contact screen, by id
The screen has the contact's id.
Already in scope: service (ArxMySqlDbService), id (ArxRecordId)
var deleted = await service.DeleteAsync("contact", id);
Console.WriteLine($"deleted: {deleted}");Check it in the database
Run it in any SQL tool connected to the database, after the code has run.
SELECT count(*)
FROM contact
WHERE email = 'rosa.delgado@example.com';Expect: 0.
2 A forget-me request, by email
A "forget me" request has only the email: delete by the key, strictly one row.
Already in scope: service (ArxMySqlDbService)
var request = new ArxEntity("contact");
request["email"] = "omar.haddad@example.com";
var deleted = await service.DeleteAsync(request, new ArxDeleteOptions { Match = ArxMatch.Key("email"), RequireSingleRow = true });
Console.WriteLine($"{deleted} contact forgotten");Check it in the database
Run it in any SQL tool connected to the database, after the code has run.
SELECT count(*)
FROM contact
WHERE email = 'omar.haddad@example.com';Expect: 0.
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
- .NET SDK: .NET SDK 10.0.100 or later 10.0.x (target
net10.0). - Create the project:
Terminal dotnet new console -n ArxSample -f net10.0 cd ArxSample - Install the packages:
Terminal dotnet add package ARX.SDK.MySQL --version 1.0.0-rc.1 - Database: a MySQL 8.4 (tested with 8.4.13) database with the sample schema.
- Configuration: set
ARX_SAMPLES_MYSQLto its connection string. Nothing is hard-coded. - Source code: add the files of the sample application this sample uses (
SampleContext.cs,SampleOutput.cs,SampleContext.Application.cs,SampleCatalog.cs), then replaceProgram.csand add the other file below, with the file names shown. - Run it:
Terminal dotnet run - Expected output: below.
The files
Runs the sample. Generated for this page; it reads the connection string from the environment.
using ArxSamples.MySQL;
using ArxSamples.MySQL.Operations.Delete.RealCases;
// The connection string comes from the environment, never from the code (see "Configure the connection").
var connectionString = Environment.GetEnvironmentVariable("ARX_SAMPLES_MYSQL")
?? throw new InvalidOperationException("Set the ARX_SAMPLES_MYSQL environment variable to your MySQL connection string.");
await new RealCase1_RemoveAContact().RunAsync(new SampleContext(connectionString));
The sample, as it is in the repository.
using ARX.SDK.Services;
using static ArxSamples.MySQL.SampleOutput;
namespace ArxSamples.MySQL.Operations.Delete.RealCases;
/// <summary>
/// Removing contacts, in two scenarios: from the contact screen (by id), and an "unsubscribe and forget me" request that
/// only gives the email address (the declared key).
/// </summary>
public sealed class RealCase1_RemoveAContact : ISample
{
public string Name => "delete-real-1-remove-a-contact";
public string Title => "Remove a contact by id or by email";
public async Task RunAsync(SampleContext dbContext)
{
var first = new ArxEntity("contact");
first["first_name"] = "Rosa";
first["last_name"] = "Delgado";
first["email"] = "rosa.delgado@example.com";
var second = new ArxEntity("contact");
second["first_name"] = "Omar";
second["last_name"] = "Haddad";
second["email"] = "omar.haddad@example.com";
var ids = await dbContext.Service.CreateMultipleAsync(new List<ArxEntity> { first, second });
await Scenario1_FromTheContactScreenAsync(dbContext, ids[0]);
await Scenario2_ForgetMeByEmailAsync(dbContext);
}
/// <summary>1. The screen has the contact's id.</summary>
private static async Task Scenario1_FromTheContactScreenAsync(SampleContext dbContext, ArxRecordId id)
{
Step("1. From the contact screen, by id");
var deleted = await dbContext.Service.DeleteAsync("contact", id);
Show($"deleted: {deleted}");
/* ===========================================================
* [DIRECT QUERY CHECK]
* -----------------------------------------------------------
* SELECT count(*)
* FROM contact
* WHERE email = 'rosa.delgado@example.com';
* -- Expect: 0.
* ===========================================================
*/
}
/// <summary>2. A "forget me" request has only the email: delete by the key, strictly one row.</summary>
private static async Task Scenario2_ForgetMeByEmailAsync(SampleContext dbContext)
{
Step("2. A forget-me request, by email");
var request = new ArxEntity("contact");
request["email"] = "omar.haddad@example.com";
var deleted = await dbContext.Service.DeleteAsync(request, new ArxDeleteOptions { Match = ArxMatch.Key("email"), RequireSingleRow = true });
Show($"{deleted} contact forgotten");
/* ===========================================================
* [DIRECT QUERY CHECK]
* -----------------------------------------------------------
* SELECT count(*)
* FROM contact
* WHERE email = 'omar.haddad@example.com';
* -- Expect: 0.
* ===========================================================
*/
}
}
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.
> 1. From the contact screen, by id
deleted: True
> 2. A forget-me request, by email
1 contact forgotten