1
0

Updated to C# 12

This commit is contained in:
2024-01-14 13:10:33 +01:00
parent 9cd1344266
commit 27cd54fb30
51 changed files with 637 additions and 379 deletions

View File

@@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFrameworks>net6.0;net8.0</TargetFrameworks>
<LangVersion>10.0</LangVersion>
<LangVersion>12.0</LangVersion>
<NrtTagMatch>efc/v[0-9]*</NrtTagMatch>
<AssemblyName>AMWD.Common.EntityFrameworkCore</AssemblyName>
@@ -22,17 +22,17 @@
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.25" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="6.0.25" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.26" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="6.0.26" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="6.0.0" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="8.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.1" />
</ItemGroup>
</Project>

View File

@@ -1,6 +1,4 @@
using System.Runtime.Serialization;
namespace System
namespace System
{
/// <summary>
/// A DatabaseProvider specific exception.
@@ -33,16 +31,16 @@ namespace System
: base(message, innerException)
{ }
#if NET6_0
#if !NET8_0_OR_GREATER
/// <summary>
/// Initializes a new instance of the <see cref="DatabaseProviderException"/> class with serialized data.
/// </summary>
/// <param name="info">The <see cref="SerializationInfo"/> that holds the serialized object data about the exception being thrown.</param>
/// <param name="context">The <see cref="StreamingContext"/> that contains contextual information about the source or destination.</param>
/// <param name="info">The <see cref="Runtime.Serialization.SerializationInfo"/> that holds the serialized object data about the exception being thrown.</param>
/// <param name="context">The <see cref="Runtime.Serialization.StreamingContext"/> that contains contextual information about the source or destination.</param>
/// <exception cref="ArgumentNullException">The info parameter is null.</exception>
/// <exception cref="SerializationException">The class name is null or <see cref="Exception.HResult"/> is zero (0).</exception>
protected DatabaseProviderException(SerializationInfo info, StreamingContext context)
/// <exception cref="Runtime.Serialization.SerializationException">The class name is null or <see cref="Exception.HResult"/> is zero (0).</exception>
protected DatabaseProviderException(Runtime.Serialization.SerializationInfo info, Runtime.Serialization.StreamingContext context)
: base(info, context)
{ }

View File

@@ -15,7 +15,11 @@ namespace Microsoft.EntityFrameworkCore
/// <summary>
/// Extensions for the <see cref="DatabaseFacade"/>.
/// </summary>
#if NET8_0_OR_GREATER
public static partial class DatabaseFacadeExtensions
#else
public static class DatabaseFacadeExtensions
#endif
{
/// <summary>
/// Applies migration files to the database.
@@ -23,7 +27,7 @@ namespace Microsoft.EntityFrameworkCore
/// <param name="database">The database connection.</param>
/// <param name="optionsAction">An action to set additional options.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>true on success, otherwise false or an exception is thrown.</returns>
/// <returns><see langword="true"/> on success, otherwise false or an exception is thrown.</returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "CA2208")]
public static async Task<bool> ApplyMigrationsAsync(this DatabaseFacade database, Action<DatabaseMigrationOptions> optionsAction, CancellationToken cancellationToken = default)
{
@@ -211,20 +215,20 @@ END;"
if (options.SourceAssembly == null)
{
availableMigrationFiles = Directory.GetFiles(options.Path)
.Where(f => f.ToLower().StartsWith(options.Path.ToLower()))
.Where(f => f.ToLower().EndsWith(".sql"))
.Where(f => f.StartsWith(options.Path, StringComparison.OrdinalIgnoreCase))
.Where(f => f.EndsWith(".sql", StringComparison.OrdinalIgnoreCase))
.ToList();
}
else
{
availableMigrationFiles = options.SourceAssembly
.GetManifestResourceNames()
.Where(f => f.ToLower().StartsWith(options.Path.ToLower()))
.Where(f => f.ToLower().EndsWith(".sql"))
.Where(f => f.StartsWith(options.Path, StringComparison.OrdinalIgnoreCase))
.Where(f => f.EndsWith(".sql", StringComparison.OrdinalIgnoreCase))
.ToList();
}
if (!availableMigrationFiles.Any())
if (availableMigrationFiles.Count == 0)
return true;
using var command = connection.CreateCommand();
@@ -270,7 +274,11 @@ END;"
{
using var stream = options.SourceAssembly.GetManifestResourceStream(migrationFile);
using var sr = new StreamReader(stream);
#if NET8_0_OR_GREATER
sqlScript = await sr.ReadToEndAsync(cancellationToken).ConfigureAwait(false);
#else
sqlScript = await sr.ReadToEndAsync().ConfigureAwait(false);
#endif
}
if (string.IsNullOrWhiteSpace(sqlScript))
@@ -316,7 +324,11 @@ END;"
{
int affectedRows = 0;
// Split script by a single slash in a line
#if NET8_0_OR_GREATER
string[] parts = FindSingleSlashInLine().Split(text);
#else
string[] parts = Regex.Split(text, @"\r?\n[ \t]*/[ \t]*\r?\n");
#endif
foreach (string part in parts)
{
// Make writable copy
@@ -325,7 +337,11 @@ END;"
// Remove the trailing semicolon from commands where they're not supported
// (Oracle doesn't like semicolons. To keep the semicolon, it must be directly
// preceeded by "end".)
pt = Regex.Replace(pt.TrimEnd(), @"(?<!end);$", "", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
#if NET8_0_OR_GREATER
pt = FindEndCommand().Replace(pt.TrimEnd(), "");
#else
pt = Regex.Replace(pt, @"(?<!end);$", "", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
#endif
// Execute all non-empty parts as individual commands
if (!string.IsNullOrWhiteSpace(pt))
@@ -352,5 +368,13 @@ END;"
SQLServer = 5,
InMemory = 6,
}
#if NET8_0_OR_GREATER
[GeneratedRegex(@"\r?\n[ \t]*/[ \t]*\r?\n")]
private static partial Regex FindSingleSlashInLine();
[GeneratedRegex(@"(?<!end);$", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant)]
private static partial Regex FindEndCommand();
#endif
}
}

View File

@@ -33,11 +33,15 @@ namespace Microsoft.EntityFrameworkCore
/// <returns>The <see cref="DbContextOptionsBuilder"/> with applied settings.</returns>
public static DbContextOptionsBuilder UseDatabaseProvider(this DbContextOptionsBuilder optionsBuilder, IConfiguration configuration, Action<DatabaseProviderOptions> optionsAction = null)
{
#if NET8_0_OR_GREATER
ArgumentNullException.ThrowIfNull(optionsBuilder);
ArgumentNullException.ThrowIfNull(configuration);
#else
if (optionsBuilder == null)
throw new ArgumentNullException(nameof(optionsBuilder));
if (configuration == null)
throw new ArgumentNullException(nameof(configuration));
#endif
var options = new DatabaseProviderOptions();
optionsAction?.Invoke(options);