feat(db): add SourceNode column + IX_AuditLog_Node_Occurred index to AuditLog
This commit is contained in:
@@ -70,6 +70,14 @@ public class AuditLogEntityTypeConfiguration : IEntityTypeConfiguration<AuditEve
|
|||||||
.HasMaxLength(256)
|
.HasMaxLength(256)
|
||||||
.IsUnicode(false);
|
.IsUnicode(false);
|
||||||
|
|
||||||
|
// SourceNode (Audit Log #23, SourceNode-stamping): node-local identifier of the
|
||||||
|
// cluster member that produced the row (e.g. "node-a", "central-a"). NULL is
|
||||||
|
// valid for reconciled rows from a retired node and for direct-write rows
|
||||||
|
// produced before this feature shipped. ASCII — varchar(64), no unicode.
|
||||||
|
builder.Property(e => e.SourceNode)
|
||||||
|
.HasColumnType("varchar(64)")
|
||||||
|
.HasMaxLength(64);
|
||||||
|
|
||||||
// Bounded unicode message column.
|
// Bounded unicode message column.
|
||||||
builder.Property(e => e.ErrorMessage)
|
builder.Property(e => e.ErrorMessage)
|
||||||
.HasMaxLength(1024);
|
.HasMaxLength(1024);
|
||||||
@@ -97,6 +105,14 @@ public class AuditLogEntityTypeConfiguration : IEntityTypeConfiguration<AuditEve
|
|||||||
.HasFilter("[ParentExecutionId] IS NOT NULL")
|
.HasFilter("[ParentExecutionId] IS NOT NULL")
|
||||||
.HasDatabaseName("IX_AuditLog_ParentExecution");
|
.HasDatabaseName("IX_AuditLog_ParentExecution");
|
||||||
|
|
||||||
|
// SourceNode composite index (Audit Log #23, SourceNode-stamping): backs
|
||||||
|
// per-node Central UI / health-dashboard queries (e.g. "rows produced by
|
||||||
|
// central-a, newest first"). Created via raw SQL in the migration so it lands
|
||||||
|
// on the ps_AuditLog_Month(OccurredAtUtc) partition scheme like every other
|
||||||
|
// IX_AuditLog_* index — keeps the partition-switch purge path intact.
|
||||||
|
builder.HasIndex(e => new { e.SourceNode, e.OccurredAtUtc })
|
||||||
|
.HasDatabaseName("IX_AuditLog_Node_Occurred");
|
||||||
|
|
||||||
builder.HasIndex(e => new { e.Channel, e.Status, e.OccurredAtUtc })
|
builder.HasIndex(e => new { e.Channel, e.Status, e.OccurredAtUtc })
|
||||||
.IsDescending(false, false, true)
|
.IsDescending(false, false, true)
|
||||||
.HasDatabaseName("IX_AuditLog_Channel_Status_Occurred");
|
.HasDatabaseName("IX_AuditLog_Channel_Status_Occurred");
|
||||||
|
|||||||
@@ -47,6 +47,11 @@ public class NotificationOutboxConfiguration : IEntityTypeConfiguration<Notifica
|
|||||||
|
|
||||||
builder.Property(n => n.SourceScript).HasMaxLength(200);
|
builder.Property(n => n.SourceScript).HasMaxLength(200);
|
||||||
|
|
||||||
|
// SourceNode (Audit Log #23, SourceNode-stamping): mapped in a follow-on migration
|
||||||
|
// (AddNotificationSourceNode). Ignored here for now so the AddAuditLogSourceNode
|
||||||
|
// migration only touches AuditLog. Removed in the AddNotificationSourceNode commit.
|
||||||
|
builder.Ignore(n => n.SourceNode);
|
||||||
|
|
||||||
// OriginExecutionId (Audit Log #23): nullable uniqueidentifier carried from the
|
// OriginExecutionId (Audit Log #23): nullable uniqueidentifier carried from the
|
||||||
// site so the dispatcher can echo it onto NotifyDeliver audit rows. No index —
|
// site so the dispatcher can echo it onto NotifyDeliver audit rows. No index —
|
||||||
// it is never a query predicate on this table, only copied onto audit events.
|
// it is never a query predicate on this table, only copied onto audit events.
|
||||||
|
|||||||
@@ -59,6 +59,11 @@ public class SiteCallEntityTypeConfiguration : IEntityTypeConfiguration<SiteCall
|
|||||||
builder.Property(s => s.LastError)
|
builder.Property(s => s.LastError)
|
||||||
.HasMaxLength(1024);
|
.HasMaxLength(1024);
|
||||||
|
|
||||||
|
// SourceNode (Audit Log #23, SourceNode-stamping): mapped in a follow-on migration
|
||||||
|
// (AddSiteCallSourceNode). Ignored here for now so the AddAuditLogSourceNode
|
||||||
|
// migration only touches AuditLog. Removed in the AddSiteCallSourceNode commit.
|
||||||
|
builder.Ignore(s => s.SourceNode);
|
||||||
|
|
||||||
// Indexes — names locked for reconciliation/migration discoverability.
|
// Indexes — names locked for reconciliation/migration discoverability.
|
||||||
// Source_Created backs "calls from this site" (Central UI Site Calls page,
|
// Source_Created backs "calls from this site" (Central UI Site Calls page,
|
||||||
// filter by SourceSite, newest first).
|
// filter by SourceSite, newest first).
|
||||||
|
|||||||
1646
src/ScadaLink.ConfigurationDatabase/Migrations/20260523201754_AddAuditLogSourceNode.Designer.cs
generated
Normal file
1646
src/ScadaLink.ConfigurationDatabase/Migrations/20260523201754_AddAuditLogSourceNode.Designer.cs
generated
Normal file
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,59 @@
|
|||||||
|
using Microsoft.EntityFrameworkCore.Migrations;
|
||||||
|
|
||||||
|
#nullable disable
|
||||||
|
|
||||||
|
namespace ScadaLink.ConfigurationDatabase.Migrations
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Adds the <c>SourceNode</c> column to the centralized <c>AuditLog</c> table (#23,
|
||||||
|
/// SourceNode-stamping). <c>SourceNode</c> identifies the cluster node that produced the
|
||||||
|
/// audit row (e.g. <c>node-a</c>, <c>central-a</c>) — ASCII-only, so <c>varchar(64)</c>
|
||||||
|
/// not <c>nvarchar</c>. <c>NULL</c> is valid (reconciled rows from a retired node,
|
||||||
|
/// central direct-write rows pre-this-feature).
|
||||||
|
///
|
||||||
|
/// The change is purely additive:
|
||||||
|
/// 1. <c>SourceNode varchar(64) NULL</c> is added with no default, so the operation
|
||||||
|
/// is a metadata-only <c>ALTER TABLE … ADD</c> — it does NOT rewrite the
|
||||||
|
/// monthly-partitioned <c>AuditLog</c> table, and historical rows stay <c>NULL</c>.
|
||||||
|
/// 2. <c>IX_AuditLog_Node_Occurred (SourceNode, OccurredAtUtc)</c> is created via raw
|
||||||
|
/// SQL so it lands on the <c>ps_AuditLog_Month(OccurredAtUtc)</c> partition scheme,
|
||||||
|
/// matching every other <c>IX_AuditLog_*</c> index. Keeping it partition-aligned
|
||||||
|
/// preserves the partition-switch purge path (see
|
||||||
|
/// AuditLogRepository.SwitchOutPartitionAsync).
|
||||||
|
/// </summary>
|
||||||
|
public partial class AddAuditLogSourceNode : Migration
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected override void Up(MigrationBuilder migrationBuilder)
|
||||||
|
{
|
||||||
|
migrationBuilder.AddColumn<string>(
|
||||||
|
name: "SourceNode",
|
||||||
|
table: "AuditLog",
|
||||||
|
type: "varchar(64)",
|
||||||
|
maxLength: 64,
|
||||||
|
nullable: true);
|
||||||
|
|
||||||
|
// Raw SQL so the index is created on the partition scheme — EF's
|
||||||
|
// CreateIndex cannot express the ON ps_AuditLog_Month(OccurredAtUtc)
|
||||||
|
// clause. Mirrors IX_AuditLog_ParentExecution (aligned, unfiltered here:
|
||||||
|
// NULL SourceNode is a legitimate query target, e.g. "rows produced
|
||||||
|
// before stamping shipped" — no HasFilter on this index).
|
||||||
|
migrationBuilder.Sql(@"
|
||||||
|
CREATE NONCLUSTERED INDEX IX_AuditLog_Node_Occurred
|
||||||
|
ON dbo.AuditLog (SourceNode, OccurredAtUtc)
|
||||||
|
ON ps_AuditLog_Month(OccurredAtUtc);");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected override void Down(MigrationBuilder migrationBuilder)
|
||||||
|
{
|
||||||
|
migrationBuilder.Sql(@"
|
||||||
|
IF EXISTS (SELECT 1 FROM sys.indexes WHERE name = 'IX_AuditLog_Node_Occurred' AND object_id = OBJECT_ID('dbo.AuditLog'))
|
||||||
|
DROP INDEX IX_AuditLog_Node_Occurred ON dbo.AuditLog;");
|
||||||
|
|
||||||
|
migrationBuilder.DropColumn(
|
||||||
|
name: "SourceNode",
|
||||||
|
table: "AuditLog");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -113,6 +113,10 @@ namespace ScadaLink.ConfigurationDatabase.Migrations
|
|||||||
.IsUnicode(false)
|
.IsUnicode(false)
|
||||||
.HasColumnType("varchar(128)");
|
.HasColumnType("varchar(128)");
|
||||||
|
|
||||||
|
b.Property<string>("SourceNode")
|
||||||
|
.HasMaxLength(64)
|
||||||
|
.HasColumnType("varchar(64)");
|
||||||
|
|
||||||
b.Property<string>("SourceScript")
|
b.Property<string>("SourceScript")
|
||||||
.HasMaxLength(128)
|
.HasMaxLength(128)
|
||||||
.IsUnicode(false)
|
.IsUnicode(false)
|
||||||
@@ -156,6 +160,9 @@ namespace ScadaLink.ConfigurationDatabase.Migrations
|
|||||||
.HasDatabaseName("IX_AuditLog_ParentExecution")
|
.HasDatabaseName("IX_AuditLog_ParentExecution")
|
||||||
.HasFilter("[ParentExecutionId] IS NOT NULL");
|
.HasFilter("[ParentExecutionId] IS NOT NULL");
|
||||||
|
|
||||||
|
b.HasIndex("SourceNode", "OccurredAtUtc")
|
||||||
|
.HasDatabaseName("IX_AuditLog_Node_Occurred");
|
||||||
|
|
||||||
b.HasIndex("SourceSiteId", "OccurredAtUtc")
|
b.HasIndex("SourceSiteId", "OccurredAtUtc")
|
||||||
.IsDescending(false, true)
|
.IsDescending(false, true)
|
||||||
.HasDatabaseName("IX_AuditLog_Site_Occurred");
|
.HasDatabaseName("IX_AuditLog_Site_Occurred");
|
||||||
|
|||||||
@@ -273,13 +273,14 @@ VALUES
|
|||||||
PayloadTruncated bit NOT NULL,
|
PayloadTruncated bit NOT NULL,
|
||||||
Extra nvarchar(max) NULL,
|
Extra nvarchar(max) NULL,
|
||||||
ForwardState varchar(32) NULL,
|
ForwardState varchar(32) NULL,
|
||||||
-- ExecutionId and ParentExecutionId are last (in this ordinal order)
|
-- ExecutionId, ParentExecutionId, and SourceNode are last (in this
|
||||||
-- because each was added to the live AuditLog table by a later
|
-- ordinal order) because each was added to the live AuditLog table
|
||||||
-- ALTER TABLE ADD migration; the staging table must match the live
|
-- by a later ALTER TABLE ADD migration; the staging table must
|
||||||
-- table column shape ordinal-for-ordinal or
|
-- match the live table column shape ordinal-for-ordinal or
|
||||||
-- ALTER TABLE ... SWITCH PARTITION fails.
|
-- ALTER TABLE ... SWITCH PARTITION fails (msg 4904/4915).
|
||||||
ExecutionId uniqueidentifier NULL,
|
ExecutionId uniqueidentifier NULL,
|
||||||
ParentExecutionId uniqueidentifier NULL,
|
ParentExecutionId uniqueidentifier NULL,
|
||||||
|
SourceNode varchar(64) NULL,
|
||||||
CONSTRAINT PK_{stagingTableName} PRIMARY KEY CLUSTERED (EventId, OccurredAtUtc)
|
CONSTRAINT PK_{stagingTableName} PRIMARY KEY CLUSTERED (EventId, OccurredAtUtc)
|
||||||
) ON [PRIMARY];
|
) ON [PRIMARY];
|
||||||
|
|
||||||
|
|||||||
@@ -74,10 +74,10 @@ public class AuditLogEntityTypeConfigurationTests : IDisposable
|
|||||||
.Where(p => !p.IsShadowProperty())
|
.Where(p => !p.IsShadowProperty())
|
||||||
.ToList();
|
.ToList();
|
||||||
|
|
||||||
// AuditEvent record exposes 23 init-only properties (alog.md §4 plus the
|
// AuditEvent record exposes 24 init-only properties (alog.md §4 plus the
|
||||||
// additive ExecutionId universal correlation column and its
|
// additive ExecutionId universal correlation column, its ParentExecutionId
|
||||||
// ParentExecutionId sibling).
|
// sibling, and the SourceNode-stamping column).
|
||||||
Assert.Equal(23, properties.Count);
|
Assert.Equal(24, properties.Count);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -93,13 +93,16 @@ public class AuditLogEntityTypeConfigurationTests : IDisposable
|
|||||||
|
|
||||||
// Five reconciliation/query indexes from alog.md §4, plus the EventId unique
|
// Five reconciliation/query indexes from alog.md §4, plus the EventId unique
|
||||||
// index introduced alongside the composite PK (Bundle C), plus the additive
|
// index introduced alongside the composite PK (Bundle C), plus the additive
|
||||||
// IX_AuditLog_Execution index supporting ExecutionId lookups and the
|
// IX_AuditLog_Execution index supporting ExecutionId lookups, the
|
||||||
// IX_AuditLog_ParentExecution index supporting ParentExecutionId lookups.
|
// IX_AuditLog_ParentExecution index supporting ParentExecutionId lookups,
|
||||||
|
// and the IX_AuditLog_Node_Occurred composite supporting per-node queries
|
||||||
|
// (SourceNode-stamping).
|
||||||
var expected = new[]
|
var expected = new[]
|
||||||
{
|
{
|
||||||
"IX_AuditLog_Channel_Status_Occurred",
|
"IX_AuditLog_Channel_Status_Occurred",
|
||||||
"IX_AuditLog_CorrelationId",
|
"IX_AuditLog_CorrelationId",
|
||||||
"IX_AuditLog_Execution",
|
"IX_AuditLog_Execution",
|
||||||
|
"IX_AuditLog_Node_Occurred",
|
||||||
"IX_AuditLog_OccurredAtUtc",
|
"IX_AuditLog_OccurredAtUtc",
|
||||||
"IX_AuditLog_ParentExecution",
|
"IX_AuditLog_ParentExecution",
|
||||||
"IX_AuditLog_Site_Occurred",
|
"IX_AuditLog_Site_Occurred",
|
||||||
|
|||||||
@@ -0,0 +1,146 @@
|
|||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace ScadaLink.ConfigurationDatabase.Tests.Migrations;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// SourceNode-stamping (#23) integration tests for the
|
||||||
|
/// <c>AddAuditLogSourceNode</c> migration: applies the EF migrations to a
|
||||||
|
/// freshly-created MSSQL test database on the running infra/mssql container and
|
||||||
|
/// asserts that the central <c>AuditLog</c> table carries the new
|
||||||
|
/// <c>SourceNode varchar(64) NULL</c> column AND a partition-aligned
|
||||||
|
/// <c>IX_AuditLog_Node_Occurred (SourceNode, OccurredAtUtc)</c> composite index.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// Mirrors the <c>AddAuditLogParentExecutionId</c> shape: column is an additive
|
||||||
|
/// metadata-only <c>ALTER TABLE … ADD</c>; the new index is created via raw SQL
|
||||||
|
/// so it lives on <c>ps_AuditLog_Month(OccurredAtUtc)</c> like every other
|
||||||
|
/// <c>IX_AuditLog_*</c> index, preserving the partition-switch purge path.
|
||||||
|
/// Tests pair <see cref="SkippableFactAttribute"/> with <c>Skip.IfNot(...)</c>
|
||||||
|
/// so the runner reports them as Skipped (not Passed) when MSSQL is unreachable.
|
||||||
|
/// The fixture applies the migrations once at construction time.
|
||||||
|
/// </remarks>
|
||||||
|
public class AddAuditLogSourceNodeMigrationTests : IClassFixture<MsSqlMigrationFixture>
|
||||||
|
{
|
||||||
|
private readonly MsSqlMigrationFixture _fixture;
|
||||||
|
|
||||||
|
public AddAuditLogSourceNodeMigrationTests(MsSqlMigrationFixture fixture)
|
||||||
|
{
|
||||||
|
_fixture = fixture;
|
||||||
|
}
|
||||||
|
|
||||||
|
[SkippableFact]
|
||||||
|
public async Task AppliesMigration_AddsSourceNodeColumn_ToAuditLog()
|
||||||
|
{
|
||||||
|
Skip.IfNot(_fixture.Available, _fixture.SkipReason);
|
||||||
|
|
||||||
|
var present = await ScalarAsync<int>(
|
||||||
|
"SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS " +
|
||||||
|
"WHERE TABLE_NAME = 'AuditLog' AND COLUMN_NAME = 'SourceNode' " +
|
||||||
|
"AND TABLE_SCHEMA = 'dbo';");
|
||||||
|
Assert.Equal(1, present);
|
||||||
|
}
|
||||||
|
|
||||||
|
[SkippableFact]
|
||||||
|
public async Task SourceNodeColumn_IsNullableVarchar64()
|
||||||
|
{
|
||||||
|
Skip.IfNot(_fixture.Available, _fixture.SkipReason);
|
||||||
|
|
||||||
|
// varchar (ASCII), not nvarchar — SourceNode is ASCII (`node-a`, `central-a` etc.)
|
||||||
|
// and design doc fixes the column at varchar(64). Catches an EF default to
|
||||||
|
// nvarchar if the migration ever drops `unicode: false`.
|
||||||
|
var dataType = await ScalarAsync<string?>(
|
||||||
|
"SELECT DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS " +
|
||||||
|
"WHERE TABLE_NAME = 'AuditLog' AND COLUMN_NAME = 'SourceNode';");
|
||||||
|
Assert.Equal("varchar", dataType);
|
||||||
|
|
||||||
|
var maxLength = await ScalarAsync<int>(
|
||||||
|
"SELECT CHARACTER_MAXIMUM_LENGTH FROM INFORMATION_SCHEMA.COLUMNS " +
|
||||||
|
"WHERE TABLE_NAME = 'AuditLog' AND COLUMN_NAME = 'SourceNode';");
|
||||||
|
Assert.Equal(64, maxLength);
|
||||||
|
|
||||||
|
var isNullable = await ScalarAsync<string?>(
|
||||||
|
"SELECT IS_NULLABLE FROM INFORMATION_SCHEMA.COLUMNS " +
|
||||||
|
"WHERE TABLE_NAME = 'AuditLog' AND COLUMN_NAME = 'SourceNode';");
|
||||||
|
Assert.Equal("YES", isNullable);
|
||||||
|
}
|
||||||
|
|
||||||
|
[SkippableFact]
|
||||||
|
public async Task AppliesMigration_CreatesIxAuditLogNodeOccurredIndex()
|
||||||
|
{
|
||||||
|
Skip.IfNot(_fixture.Available, _fixture.SkipReason);
|
||||||
|
|
||||||
|
// Locked index name from the design doc / CLAUDE.md.
|
||||||
|
var indexCount = await ScalarAsync<int>(
|
||||||
|
"SELECT COUNT(*) FROM sys.indexes i " +
|
||||||
|
"INNER JOIN sys.objects o ON i.object_id = o.object_id " +
|
||||||
|
"WHERE o.name = 'AuditLog' AND i.name = 'IX_AuditLog_Node_Occurred';");
|
||||||
|
Assert.Equal(1, indexCount);
|
||||||
|
}
|
||||||
|
|
||||||
|
[SkippableFact]
|
||||||
|
public async Task IxAuditLogNodeOccurred_HasExpectedKeyColumnsInOrder()
|
||||||
|
{
|
||||||
|
Skip.IfNot(_fixture.Available, _fixture.SkipReason);
|
||||||
|
|
||||||
|
// Key columns in order: SourceNode, OccurredAtUtc. sys.index_columns.key_ordinal
|
||||||
|
// gives the position in the index key (1-based); is_included_column = 0 means
|
||||||
|
// it's part of the key, not an INCLUDE.
|
||||||
|
var keyColumns = new List<(int Ordinal, string Name)>();
|
||||||
|
|
||||||
|
await using (var conn = _fixture.OpenConnection())
|
||||||
|
await using (var cmd = conn.CreateCommand())
|
||||||
|
{
|
||||||
|
cmd.CommandText =
|
||||||
|
"SELECT ic.key_ordinal, c.name " +
|
||||||
|
"FROM sys.indexes i " +
|
||||||
|
"INNER JOIN sys.objects o ON i.object_id = o.object_id " +
|
||||||
|
"INNER JOIN sys.index_columns ic ON ic.object_id = i.object_id AND ic.index_id = i.index_id " +
|
||||||
|
"INNER JOIN sys.columns c ON c.object_id = ic.object_id AND c.column_id = ic.column_id " +
|
||||||
|
"WHERE o.name = 'AuditLog' AND i.name = 'IX_AuditLog_Node_Occurred' " +
|
||||||
|
" AND ic.is_included_column = 0 " +
|
||||||
|
"ORDER BY ic.key_ordinal;";
|
||||||
|
|
||||||
|
await using var reader = await cmd.ExecuteReaderAsync();
|
||||||
|
while (await reader.ReadAsync())
|
||||||
|
{
|
||||||
|
keyColumns.Add((reader.GetByte(0), reader.GetString(1)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Assert.Equal(2, keyColumns.Count);
|
||||||
|
Assert.Equal("SourceNode", keyColumns[0].Name);
|
||||||
|
Assert.Equal("OccurredAtUtc", keyColumns[1].Name);
|
||||||
|
}
|
||||||
|
|
||||||
|
[SkippableFact]
|
||||||
|
public async Task IxAuditLogNodeOccurred_LivesOnPsAuditLogMonth_PartitionScheme()
|
||||||
|
{
|
||||||
|
Skip.IfNot(_fixture.Available, _fixture.SkipReason);
|
||||||
|
|
||||||
|
// Partition-aligned indexes are required so the AuditLog partition-switch
|
||||||
|
// purge keeps working. Every other IX_AuditLog_* index lives on
|
||||||
|
// ps_AuditLog_Month(OccurredAtUtc); the new one must too.
|
||||||
|
var schemeName = await ScalarAsync<string?>(
|
||||||
|
"SELECT ps.name FROM sys.indexes i " +
|
||||||
|
"INNER JOIN sys.objects o ON i.object_id = o.object_id " +
|
||||||
|
"INNER JOIN sys.partition_schemes ps ON i.data_space_id = ps.data_space_id " +
|
||||||
|
"WHERE o.name = 'AuditLog' AND i.name = 'IX_AuditLog_Node_Occurred';");
|
||||||
|
|
||||||
|
Assert.Equal("ps_AuditLog_Month", schemeName);
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- helpers ------------------------------------------------------------
|
||||||
|
|
||||||
|
private async Task<T> ScalarAsync<T>(string sql)
|
||||||
|
{
|
||||||
|
await using var conn = _fixture.OpenConnection();
|
||||||
|
await using var cmd = conn.CreateCommand();
|
||||||
|
cmd.CommandText = sql;
|
||||||
|
var result = await cmd.ExecuteScalarAsync();
|
||||||
|
if (result is null || result is DBNull)
|
||||||
|
{
|
||||||
|
return default!;
|
||||||
|
}
|
||||||
|
return (T)Convert.ChangeType(result, typeof(T) == typeof(string) ? typeof(string) : Nullable.GetUnderlyingType(typeof(T)) ?? typeof(T))!;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user