Update to .NET 6 and C# 10 (#5233)

Co-authored-by: Pieter-Jan Briers <pieterjan.briers+git@gmail.com>
This commit is contained in:
Javier Guardia Fernández
2021-11-09 15:05:07 +01:00
committed by GitHub
parent 29f3d2d1ca
commit af579b15cc
23 changed files with 63 additions and 43 deletions

View File

@@ -3,7 +3,7 @@
<PropertyGroup>
<!-- Work around https://github.com/dotnet/project-system/issues/4314 -->
<TargetFramework>$(TargetFramework)</TargetFramework>
<LangVersion>9</LangVersion>
<LangVersion>10</LangVersion>
<IsPackable>false</IsPackable>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<OutputPath>..\bin\Content.Server.Database\</OutputPath>
@@ -13,12 +13,12 @@
<Import Project="..\RobustToolbox\MSBuild\Robust.DefineConstants.targets" />
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.3">
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="5.0.3" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="5.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.0" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="6.0.0-rc.2" />
</ItemGroup>
<Import Project="..\RobustToolbox\MSBuild\Robust.Analyzers.targets" />
</Project>

View File

@@ -2,6 +2,7 @@
using System.ComponentModel.DataAnnotations.Schema;
using System.Net;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage;
@@ -28,6 +29,11 @@ namespace Content.Server.Database
options.ReplaceService<IRelationalTypeMappingSource, CustomNpgsqlTypeMappingSource>();
((IDbContextOptionsBuilderInfrastructure) options).AddOrUpdateExtension(new SnakeCaseExtension());
options.ConfigureWarnings(x =>
{
x.Ignore(CoreEventId.ManyServiceProvidersCreatedWarning);
});
}
public PostgresServerDbContext(DbContextOptions<ServerDbContext> options) : base(options)
@@ -82,7 +88,7 @@ namespace Content.Server.Database
{
foreach(var property in entity.GetProperties())
{
if (property.FieldInfo.FieldType == typeof(DateTime) || property.FieldInfo.FieldType == typeof(DateTime?))
if (property.FieldInfo?.FieldType == typeof(DateTime) || property.FieldInfo?.FieldType == typeof(DateTime?))
property.SetColumnType("timestamp with time zone");
}
}

View File

@@ -3,6 +3,7 @@ using System.ComponentModel.DataAnnotations.Schema;
using System.Globalization;
using System.Net;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
@@ -25,6 +26,11 @@ namespace Content.Server.Database
options.UseSqlite("dummy connection string");
((IDbContextOptionsBuilderInfrastructure) options).AddOrUpdateExtension(new SnakeCaseExtension());
options.ConfigureWarnings(x =>
{
x.Ignore(CoreEventId.ManyServiceProvidersCreatedWarning);
});
}
protected override void OnModelCreating(ModelBuilder modelBuilder)

View File

@@ -33,7 +33,12 @@ namespace Content.Server.Database
public override string LogFragment => "Snake Case Extension";
public override long GetServiceProviderHashCode() => 0;
public override int GetServiceProviderHashCode() => 0;
public override bool ShouldUseSameServiceProvider(DbContextOptionsExtensionInfo other)
{
return false;
}
public override void PopulateDebugInfo(IDictionary<string, string> debugInfo)
{
@@ -118,26 +123,26 @@ namespace Content.Server.Database
if (entityType.BaseType is null)
{
entityTypeBuilder.ToTable(RewriteName(entityType.GetTableName()), entityType.GetSchema());
entityTypeBuilder.ToTable(RewriteName(entityType.GetTableName()!), entityType.GetSchema());
if (entityType.GetViewNameConfigurationSource() == ConfigurationSource.Convention)
{
entityTypeBuilder.ToView(RewriteName(entityType.GetViewName()), entityType.GetViewSchema());
entityTypeBuilder.ToView(RewriteName(entityType.GetViewName()!), entityType.GetViewSchema());
}
}
}
public void ProcessEntityTypeBaseTypeChanged(
IConventionEntityTypeBuilder entityTypeBuilder,
IConventionEntityType newBaseType,
IConventionEntityType oldBaseType,
IConventionEntityType? newBaseType,
IConventionEntityType? oldBaseType,
IConventionContext<IConventionEntityType> context)
{
var entityType = entityTypeBuilder.Metadata;
if (newBaseType is null)
{
entityTypeBuilder.ToTable(RewriteName(entityType.GetTableName()), entityType.GetSchema());
entityTypeBuilder.ToTable(RewriteName(entityType.GetTableName()!), entityType.GetSchema());
}
else
{
@@ -173,8 +178,8 @@ namespace Content.Server.Database
public void ProcessEntityTypeAnnotationChanged(
IConventionEntityTypeBuilder entityTypeBuilder,
string name,
IConventionAnnotation annotation,
IConventionAnnotation oldAnnotation,
IConventionAnnotation? annotation,
IConventionAnnotation? oldAnnotation,
IConventionContext<IConventionAnnotation> context)
{
var entityType = entityTypeBuilder.Metadata;
@@ -216,7 +221,7 @@ namespace Content.Server.Database
&& (string)annotation.Value != ownership.PrincipalEntityType.GetTableName())
{
foreach (var property in entityType.GetProperties()
.Except(entityType.FindPrimaryKey().Properties)
.Except(entityType.FindPrimaryKey()!.Properties)
.Where(p => p.Builder.CanSetColumnName(null)))
{
RewriteColumnName(property.Builder);
@@ -271,7 +276,7 @@ namespace Content.Server.Database
if (property.GetColumnNameConfigurationSource(identifier.Value) == ConfigurationSource.Convention)
{
columnName = property.GetColumnName(identifier.Value);
columnName = property.GetColumnName(identifier.Value)!;
if (columnName.StartsWith(entityType.ShortName() + '_', StringComparison.Ordinal))
{
property.Builder.HasColumnName(
@@ -314,7 +319,7 @@ namespace Content.Server.Database
if (name == "Id")
name = entityType.GetTableName() + name;
propertyBuilder.HasColumnName(
RewriteName(name), identifier.Value);
RewriteName(name!), identifier.Value);
}
}
}