save work
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
using System.Globalization;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using Microsoft.Extensions.Logging;
|
||||
@@ -234,7 +235,30 @@ public abstract class RepositoryBase<TDataSource> where TDataSource : DataSource
|
||||
configureCommand?.Invoke(command);
|
||||
|
||||
var result = await command.ExecuteScalarAsync(cancellationToken).ConfigureAwait(false);
|
||||
return result is DBNull or null ? default : (T)result;
|
||||
if (result is DBNull or null)
|
||||
{
|
||||
return default;
|
||||
}
|
||||
|
||||
if (result is T typed)
|
||||
{
|
||||
return typed;
|
||||
}
|
||||
|
||||
var targetType = typeof(T);
|
||||
var underlyingTargetType = Nullable.GetUnderlyingType(targetType) ?? targetType;
|
||||
|
||||
try
|
||||
{
|
||||
var converted = Convert.ChangeType(result, underlyingTargetType, CultureInfo.InvariantCulture);
|
||||
return (T?)converted;
|
||||
}
|
||||
catch (Exception ex) when (ex is InvalidCastException or FormatException or OverflowException)
|
||||
{
|
||||
throw new InvalidCastException(
|
||||
$"Failed to convert scalar result ({result.GetType().FullName}) to {targetType.FullName} in {callerName ?? "unknown"}.",
|
||||
ex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
Reference in New Issue
Block a user