Skip to content

Commit db3b742

Browse files
[main] Source code updates from dotnet/efcore (#4119)
[main] Source code updates from dotnet/efcore
1 parent c658f95 commit db3b742

File tree

19 files changed

+175
-63
lines changed

19 files changed

+175
-63
lines changed

src/efcore/src/EFCore.Analyzers/EFCore.Analyzers.csproj

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,18 @@
55
<TargetFramework>netstandard2.0</TargetFramework>
66
<AssemblyName>Microsoft.EntityFrameworkCore.Analyzers</AssemblyName>
77
<RootNamespace>Microsoft.EntityFrameworkCore</RootNamespace>
8-
<IncludeBuildOutput>false</IncludeBuildOutput>
9-
<NuspecFile>$(MSBuildProjectName).nuspec</NuspecFile>
8+
9+
<!-- Causes the dll/pdb to be placed in the analyzers/dotnet/cs folder in the nupkg instead of lib -->
10+
<BuildOutputTargetFolder>analyzers/dotnet/cs</BuildOutputTargetFolder>
11+
12+
<!-- A slightly hacky way to place the dll/pdbs directly under analyzers/dotnet/cs, and not under a netstandard2.0 subfolder -->
13+
<IsTool>true</IsTool>
14+
15+
<IncludeSymbols>true</IncludeSymbols>
1016
<CodeAnalysisRuleSet>$(MSBuildThisFileDirectory)..\..\rulesets\EFCore.noxmldocs.ruleset</CodeAnalysisRuleSet>
1117
<ImplicitUsings>true</ImplicitUsings>
1218
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
19+
<PackageReadmeFile>README.md</PackageReadmeFile>
1320
<NoWarn>$(NoWarn);NU5128</NoWarn>
1421
<NoWarn>$(NoWarn);RS1038</NoWarn> <!-- Compiler extensions should be implemented in assemblies with compiler-provided references, #35752 -->
1522
</PropertyGroup>
@@ -34,19 +41,6 @@
3441
<PackageReference Update="NETStandard.Library" PrivateAssets="all" />
3542
</ItemGroup>
3643

37-
<Target Name="SetPackageProperties" BeforeTargets="InitializeStandardNuspecProperties" DependsOnTargets="Build">
38-
<PropertyGroup>
39-
<!-- Make sure we create a symbols.nupkg. -->
40-
<IncludeSymbols>true</IncludeSymbols>
41-
</PropertyGroup>
42-
43-
<ItemGroup>
44-
<NuspecProperty Include="targetFramework=$(TargetFramework)" />
45-
<NuspecProperty Include="OutputBinary=$(OutputPath)$(AssemblyName).dll" />
46-
<NuspecProperty Include="OutputSymbol=$(OutputPath)$(AssemblyName).pdb" />
47-
</ItemGroup>
48-
</Target>
49-
5044
<ItemGroup>
5145
<AdditionalFiles Include="AnalyzerReleases.Shipped.md" />
5246
<AdditionalFiles Include="AnalyzerReleases.Unshipped.md" />

src/efcore/src/EFCore.Analyzers/EFCore.Analyzers.nuspec

Lines changed: 0 additions & 18 deletions
This file was deleted.
File renamed without changes.

src/efcore/src/EFCore.Relational/Migrations/MigrationsSqlGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1367,7 +1367,7 @@ protected virtual void ColumnDefinition(
13671367
{
13681368
builder
13691369
.Append(" COLLATE ")
1370-
.Append(operation.Collation);
1370+
.Append(Dependencies.SqlGenerationHelper.DelimitIdentifier(operation.Collation));
13711371
}
13721372

13731373
builder.Append(operation.IsNullable ? " NULL" : " NOT NULL");

src/efcore/src/EFCore.Relational/Query/QuerySqlGenerator.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -733,9 +733,13 @@ protected override Expression VisitCollate(CollateExpression collateExpression)
733733
{
734734
Visit(collateExpression.Operand);
735735

736+
// In some databases (e.g. SQL Server), collations aren't regular identifiers (like column or table names), and so shouldn't be
737+
// quoted. However, we still default to quoting to prevent cases where arbitrary collation name inputs get integrated into the SQL.
738+
// Providers which don't support quoting of collation names should override this method to provide sanitization logic,
739+
// e.g. throwing if the name contains any but a restricted set of characters.
736740
_relationalCommandBuilder
737741
.Append(" COLLATE ")
738-
.Append(collateExpression.Collation);
742+
.Append(_sqlGenerationHelper.DelimitIdentifier(collateExpression.Collation));
739743

740744
return collateExpression;
741745
}

src/efcore/src/EFCore.SqlServer/Migrations/SqlServerMigrationsSqlGenerator.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1646,6 +1646,18 @@ protected override void ColumnDefinition(
16461646

16471647
if (operation.Collation != null)
16481648
{
1649+
// SQL Server collation docs: https://learn.microsoft.com/sql/relational-databases/collations/collation-and-unicode-support
1650+
1651+
// The default behavior in MigrationsSqlGenerator is to quote collation names, but SQL Server does not support that.
1652+
// Instead, make sure the collation name only contains a restricted set of characters.
1653+
foreach (var c in operation.Collation)
1654+
{
1655+
if (!char.IsLetterOrDigit(c) && c != '_')
1656+
{
1657+
throw new InvalidOperationException(SqlServerStrings.InvalidCollationName(operation.Collation));
1658+
}
1659+
}
1660+
16491661
builder
16501662
.Append(" COLLATE ")
16511663
.Append(operation.Collation);

src/efcore/src/EFCore.SqlServer/Properties/SqlServerStrings.Designer.cs

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/efcore/src/EFCore.SqlServer/Properties/SqlServerStrings.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,9 @@
192192
<data name="IndexTableRequired" xml:space="preserve">
193193
<value>SQL Server requires the table name to be specified for index operations. Specify table name in calls to 'MigrationBuilder.RenameIndex' and 'DropIndex'.</value>
194194
</data>
195+
<data name="InvalidCollationName" xml:space="preserve">
196+
<value>Collation name '{collation}' is invalid; collation names may only contain alphanumeric characters and underscores.</value>
197+
</data>
195198
<data name="InvalidColumnNameForFreeText" xml:space="preserve">
196199
<value>The expression passed to the 'propertyReference' parameter of the 'FreeText' method is not a valid reference to a property. The expression must represent a reference to a full-text indexed property on the object referenced in the from clause: 'from e in context.Entities where EF.Functions.FreeText(e.SomeProperty, textToSearchFor) select e'</value>
197200
</data>

src/efcore/src/EFCore.SqlServer/Query/Internal/SqlServerQuerySqlGenerator.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,35 @@ protected override bool TryGenerateWithoutWrappingSelect(SelectExpression select
5555
=> selectExpression.Tables is not [ValuesExpression]
5656
&& base.TryGenerateWithoutWrappingSelect(selectExpression);
5757

58+
/// <summary>
59+
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
60+
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
61+
/// any release. You should only use it directly in your code with extreme caution and knowing that
62+
/// doing so can result in application failures when updating to a new Entity Framework Core release.
63+
/// </summary>
64+
protected override Expression VisitCollate(CollateExpression collateExpression)
65+
{
66+
Visit(collateExpression.Operand);
67+
68+
// SQL Server collation docs: https://learn.microsoft.com/sql/relational-databases/collations/collation-and-unicode-support
69+
70+
// The default behavior in QuerySqlGenerator is to quote collation names, but SQL Server does not support that.
71+
// Instead, make sure the collation name only contains a restricted set of characters.
72+
foreach (var c in collateExpression.Collation)
73+
{
74+
if (!char.IsLetterOrDigit(c) && c != '_')
75+
{
76+
throw new InvalidOperationException(SqlServerStrings.InvalidCollationName(collateExpression.Collation));
77+
}
78+
}
79+
80+
Sql
81+
.Append(" COLLATE ")
82+
.Append(collateExpression.Collation);
83+
84+
return collateExpression;
85+
}
86+
5887
/// <summary>
5988
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
6089
/// the same compatibility standards as public APIs. It may be changed or removed without notice in

src/efcore/src/EFCore.Sqlite.Core/Migrations/SqliteMigrationsSqlGenerator.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -377,13 +377,13 @@ private IReadOnlyList<MigrationOperation> RewriteOperations(
377377
// Skip autoincrement primary key columns that are being added in this migration
378378
var isAutoincrement = column.FindAnnotation(SqliteAnnotationNames.Autoincrement)?.Value as bool? == true;
379379
var isPrimaryKey = column.Table.PrimaryKey?.Columns.Contains(column) == true;
380-
380+
381381
if (isAutoincrement && isPrimaryKey)
382382
{
383383
// Check if this column is being added in the current migration
384384
var isNewColumn = migrationOperations.OfType<AddColumnOperation>()
385385
.Any(op => op.Table == key.Table && op.Schema == key.Schema && op.Name == column.Name);
386-
386+
387387
if (isNewColumn)
388388
{
389389
continue; // Skip newly added autoincrement columns
@@ -976,7 +976,7 @@ protected override void ComputedColumnDefinition(
976976
{
977977
builder
978978
.Append(" COLLATE ")
979-
.Append(operation.Collation);
979+
.Append(Dependencies.SqlGenerationHelper.DelimitIdentifier(operation.Collation));
980980
}
981981
}
982982

0 commit comments

Comments
 (0)