Skip to content

Commit

Permalink
Fix NativeAOT warnings in Microsoft.Data.Sqlite
Browse files Browse the repository at this point in the history
Part of #29761
  • Loading branch information
AndriySvyryd committed Jul 21, 2023
1 parent 5274ba3 commit d6fdc4d
Show file tree
Hide file tree
Showing 18 changed files with 182 additions and 83 deletions.
10 changes: 3 additions & 7 deletions All.sln
Original file line number Diff line number Diff line change
Expand Up @@ -334,14 +334,14 @@ Global
{F1B2E5A0-8C74-414A-B262-353FEE325E9F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F1B2E5A0-8C74-414A-B262-353FEE325E9F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F1B2E5A0-8C74-414A-B262-353FEE325E9F}.Release|Any CPU.Build.0 = Release|Any CPU
{59C1CEDE-AA45-41FE-8098-6279A72C421A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{59C1CEDE-AA45-41FE-8098-6279A72C421A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1FE385D8-8F8B-4EC9-A1A9-AFCC38B8546C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1FE385D8-8F8B-4EC9-A1A9-AFCC38B8546C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1FE385D8-8F8B-4EC9-A1A9-AFCC38B8546C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1FE385D8-8F8B-4EC9-A1A9-AFCC38B8546C}.Release|Any CPU.Build.0 = Release|Any CPU
{2487950B-403A-482C-8ED3-CCF31E9E677F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2487950B-403A-482C-8ED3-CCF31E9E677F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2487950B-403A-482C-8ED3-CCF31E9E677F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2487950B-403A-482C-8ED3-CCF31E9E677F}.Release|Any CPU.Build.0 = Release|Any CPU
{8F722A02-71A4-4787-ACD8-FB7D5B7AE648}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8F722A02-71A4-4787-ACD8-FB7D5B7AE648}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8F722A02-71A4-4787-ACD8-FB7D5B7AE648}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand All @@ -358,10 +358,6 @@ Global
{2AC6A8AC-5C0A-422A-B21A-CDC8D75F20A3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2AC6A8AC-5C0A-422A-B21A-CDC8D75F20A3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2AC6A8AC-5C0A-422A-B21A-CDC8D75F20A3}.Release|Any CPU.Build.0 = Release|Any CPU
{59C1CEDE-AA45-41FE-8098-6279A72C421A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{59C1CEDE-AA45-41FE-8098-6279A72C421A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{59C1CEDE-AA45-41FE-8098-6279A72C421A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{59C1CEDE-AA45-41FE-8098-6279A72C421A}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -415,13 +411,13 @@ Global
{F956A344-5C8D-4015-A3BF-7A8304C58BE4} = {258D5057-81B9-40EC-A872-D21E27452749}
{CC93C465-F5AC-4CB9-A064-3675955962F4} = {258D5057-81B9-40EC-A872-D21E27452749}
{F1B2E5A0-8C74-414A-B262-353FEE325E9F} = {258D5057-81B9-40EC-A872-D21E27452749}
{59C1CEDE-AA45-41FE-8098-6279A72C421A} = {258D5057-81B9-40EC-A872-D21E27452749}
{1FE385D8-8F8B-4EC9-A1A9-AFCC38B8546C} = {CE6B50B2-34AE-44C9-940A-4E48C3E1B3BC}
{2487950B-403A-482C-8ED3-CCF31E9E677F} = {258D5057-81B9-40EC-A872-D21E27452749}
{8F722A02-71A4-4787-ACD8-FB7D5B7AE648} = {CE6B50B2-34AE-44C9-940A-4E48C3E1B3BC}
{01F86E65-6448-424C-AAB5-9C6427EF6FD4} = {258D5057-81B9-40EC-A872-D21E27452749}
{3D935B7D-80BD-49AD-BDC9-E1B0C9D9494F} = {CE6B50B2-34AE-44C9-940A-4E48C3E1B3BC}
{2AC6A8AC-5C0A-422A-B21A-CDC8D75F20A3} = {258D5057-81B9-40EC-A872-D21E27452749}
{59C1CEDE-AA45-41FE-8098-6279A72C421A} = {258D5057-81B9-40EC-A872-D21E27452749}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {285A5EB4-BCF4-40EB-B9E1-DF6DBCB5E705}
Expand Down
1 change: 0 additions & 1 deletion eng/testing/linker/SupportFiles/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,3 @@ dotnet_analyzer_diagnostic.category-AOT.severity = error
dotnet_analyzer_diagnostic.category-Trimming.severity = error
dotnet_diagnostic.SA0001.severity = none
dotnet_diagnostic.SA1516.severity = none

4 changes: 2 additions & 2 deletions eng/testing/linker/trimmingTests.targets
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
<PropertyGroup>
<_additionalProjectReferencesString>@(_additionalProjectReference, '%0a ')</_additionalProjectReferencesString>
</PropertyGroup>

<ItemGroup Condition="'@(PackageReference)' != ''">
<PackageReference Condition="'%(PackageReference.PrivateAssets)' == ''">
<PrivateAssets>contentfiles;analyzers;build</PrivateAssets>
Expand Down Expand Up @@ -150,7 +150,7 @@
Outputs="_unused">

<Message Text="[Trimming Tests] Running test: %(TestConsoleApps.ProjectCompileItems) ..." />
<Exec IgnoreExitCode="true" Command="%(TestConsoleApps.TestCommand)" StandardOutputImportance="Low" WorkingDirectory="%(TestConsoleApps.TestExecutionDirectory)">
<Exec IgnoreExitCode="true" Command="%(TestConsoleApps.TestCommand)" StandardOutputImportance="Normal" WorkingDirectory="%(TestConsoleApps.TestExecutionDirectory)">
<Output TaskParameter="ExitCode" PropertyName="ExecutionExitCode" />
</Exec>

Expand Down
5 changes: 4 additions & 1 deletion src/EFCore/Internal/DbContextServices.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Runtime.CompilerServices;
using Microsoft.EntityFrameworkCore.Infrastructure.Internal;

namespace Microsoft.EntityFrameworkCore.Internal;
Expand Down Expand Up @@ -85,7 +86,9 @@ private IModel CreateModel(bool designTime)

return modelFromOptions == null
|| (designTime && modelFromOptions is not Metadata.Internal.Model)
? dependencies.ModelSource.GetModel(_currentContext!.Context, dependencies, designTime)
? RuntimeFeature.IsDynamicCodeSupported
? dependencies.ModelSource.GetModel(_currentContext!.Context, dependencies, designTime)
: throw new InvalidOperationException(CoreStrings.NativeAotNoCompiledModel)
: dependencies.ModelRuntimeInitializer.Initialize(modelFromOptions, designTime, dependencies.ValidationLogger);
}
finally
Expand Down
6 changes: 6 additions & 0 deletions src/EFCore/Properties/CoreStrings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/EFCore/Properties/CoreStrings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1083,6 +1083,9 @@
<data name="NamedIndexWrongType" xml:space="preserve">
<value>The index with name {indexName} cannot be removed from the entity type '{entityType}' because no such index exists on that entity type.</value>
</data>
<data name="NativeAotNoCompiledModel" xml:space="preserve">
<value>Model building is not supported when publishing with NativeAOT. Use a compiled model.</value>
</data>
<data name="NavigationArray" xml:space="preserve">
<value>The type of navigation '{1_entityType}.{0_navigation}' is '{foundType}' which is an array type. Collection navigations cannot be arrays.</value>
</data>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ Microsoft.Data.Sqlite.SqliteException
Microsoft.Data.Sqlite.SqliteFactory
Microsoft.Data.Sqlite.SqliteParameter
Microsoft.Data.Sqlite.SqliteTransaction</Description>
<TargetFrameworks>netstandard2.0;net6.0</TargetFrameworks>
<TargetFrameworks>$(DefaultNetCoreTargetFramework);netstandard2.0;net6.0</TargetFrameworks>
<MinClientVersion>3.6</MinClientVersion>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<CodeAnalysisRuleSet>Microsoft.Data.Sqlite.Core.ruleset</CodeAnalysisRuleSet>
<PackageTags>SQLite;Data;ADO.NET</PackageTags>
<PackageProjectUrl>https://docs.microsoft.com/dotnet/standard/data/sqlite/</PackageProjectUrl>
<IsAotCompatible>true</IsAotCompatible>
</PropertyGroup>

<ItemGroup>
Expand Down
132 changes: 72 additions & 60 deletions src/Microsoft.Data.Sqlite.Core/SqliteDataReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ public override string GetDataTypeName(int ordinal)
/// </summary>
/// <param name="ordinal">The zero-based column ordinal.</param>
/// <returns>The data type of the column.</returns>
#if NET6_0_OR_GREATER
#if NET8_0_OR_GREATER
[return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.PublicFields)]
#endif
public override Type GetFieldType(int ordinal)
Expand Down Expand Up @@ -623,75 +623,74 @@ public override DataTable GetSchemaTable()

var schemaTable = new DataTable("SchemaTable");

var ColumnName = new DataColumn(SchemaTableColumn.ColumnName, typeof(string));
var ColumnOrdinal = new DataColumn(SchemaTableColumn.ColumnOrdinal, typeof(int));
var ColumnSize = new DataColumn(SchemaTableColumn.ColumnSize, typeof(int));
var NumericPrecision = new DataColumn(SchemaTableColumn.NumericPrecision, typeof(short));
var NumericScale = new DataColumn(SchemaTableColumn.NumericScale, typeof(short));

var DataType = new DataColumn(SchemaTableColumn.DataType, typeof(Type));
var DataTypeName = new DataColumn("DataTypeName", typeof(string));
var columnNameColumn = new DataColumn(SchemaTableColumn.ColumnName, typeof(string));
var columnOrdinalColumn = new DataColumn(SchemaTableColumn.ColumnOrdinal, typeof(int));
var columnSizeColumn = new DataColumn(SchemaTableColumn.ColumnSize, typeof(int));
var numericPrecisionColumn = new DataColumn(SchemaTableColumn.NumericPrecision, typeof(short));
var numericScaleColumn = new DataColumn(SchemaTableColumn.NumericScale, typeof(short));
var dataTypeColumn = CreateDataTypeColumn();
var dataTypeNameColumn = new DataColumn("DataTypeName", typeof(string));

var IsLong = new DataColumn(SchemaTableColumn.IsLong, typeof(bool));
var AllowDBNull = new DataColumn(SchemaTableColumn.AllowDBNull, typeof(bool));
var isLongColumn = new DataColumn(SchemaTableColumn.IsLong, typeof(bool));
var allowDBNullColumn = new DataColumn(SchemaTableColumn.AllowDBNull, typeof(bool));

var IsUnique = new DataColumn(SchemaTableColumn.IsUnique, typeof(bool));
var IsKey = new DataColumn(SchemaTableColumn.IsKey, typeof(bool));
var IsAutoIncrement = new DataColumn(SchemaTableOptionalColumn.IsAutoIncrement, typeof(bool));
var isUniqueColumn = new DataColumn(SchemaTableColumn.IsUnique, typeof(bool));
var isKeyColumn = new DataColumn(SchemaTableColumn.IsKey, typeof(bool));
var isAutoIncrementColumn = new DataColumn(SchemaTableOptionalColumn.IsAutoIncrement, typeof(bool));

var BaseCatalogName = new DataColumn(SchemaTableOptionalColumn.BaseCatalogName, typeof(string));
var BaseSchemaName = new DataColumn(SchemaTableColumn.BaseSchemaName, typeof(string));
var BaseTableName = new DataColumn(SchemaTableColumn.BaseTableName, typeof(string));
var BaseColumnName = new DataColumn(SchemaTableColumn.BaseColumnName, typeof(string));
var baseCatalogNameColumn = new DataColumn(SchemaTableOptionalColumn.BaseCatalogName, typeof(string));
var baseSchemaNameColumn = new DataColumn(SchemaTableColumn.BaseSchemaName, typeof(string));
var baseTableNameColumn = new DataColumn(SchemaTableColumn.BaseTableName, typeof(string));
var baseColumnNameColumn = new DataColumn(SchemaTableColumn.BaseColumnName, typeof(string));

var BaseServerName = new DataColumn(SchemaTableOptionalColumn.BaseServerName, typeof(string));
var IsAliased = new DataColumn(SchemaTableColumn.IsAliased, typeof(bool));
var IsExpression = new DataColumn(SchemaTableColumn.IsExpression, typeof(bool));
var baseServerNameColumn = new DataColumn(SchemaTableOptionalColumn.BaseServerName, typeof(string));
var isAliasedColumn = new DataColumn(SchemaTableColumn.IsAliased, typeof(bool));
var isExpressionColumn = new DataColumn(SchemaTableColumn.IsExpression, typeof(bool));

var columns = schemaTable.Columns;

columns.Add(ColumnName);
columns.Add(ColumnOrdinal);
columns.Add(ColumnSize);
columns.Add(NumericPrecision);
columns.Add(NumericScale);
columns.Add(IsUnique);
columns.Add(IsKey);
columns.Add(BaseServerName);
columns.Add(BaseCatalogName);
columns.Add(BaseColumnName);
columns.Add(BaseSchemaName);
columns.Add(BaseTableName);
columns.Add(DataType);
columns.Add(DataTypeName);
columns.Add(AllowDBNull);
columns.Add(IsAliased);
columns.Add(IsExpression);
columns.Add(IsAutoIncrement);
columns.Add(IsLong);
columns.Add(columnNameColumn);
columns.Add(columnOrdinalColumn);
columns.Add(columnSizeColumn);
columns.Add(numericPrecisionColumn);
columns.Add(numericScaleColumn);
columns.Add(isUniqueColumn);
columns.Add(isKeyColumn);
columns.Add(baseServerNameColumn);
columns.Add(baseCatalogNameColumn);
columns.Add(baseColumnNameColumn);
columns.Add(baseSchemaNameColumn);
columns.Add(baseTableNameColumn);
columns.Add(dataTypeColumn);
columns.Add(dataTypeNameColumn);
columns.Add(allowDBNullColumn);
columns.Add(isAliasedColumn);
columns.Add(isExpressionColumn);
columns.Add(isAutoIncrementColumn);
columns.Add(isLongColumn);

for (var i = 0; i < FieldCount; i++)
{
var schemaRow = schemaTable.NewRow();
schemaRow[ColumnName] = GetName(i);
schemaRow[ColumnOrdinal] = i;
schemaRow[ColumnSize] = -1;
schemaRow[NumericPrecision] = DBNull.Value;
schemaRow[NumericScale] = DBNull.Value;
schemaRow[BaseServerName] = _command.Connection!.DataSource;
schemaRow[columnNameColumn] = GetName(i);
schemaRow[columnOrdinalColumn] = i;
schemaRow[columnSizeColumn] = -1;
schemaRow[numericPrecisionColumn] = DBNull.Value;
schemaRow[numericScaleColumn] = DBNull.Value;
schemaRow[baseServerNameColumn] = _command.Connection!.DataSource;
var databaseName = sqlite3_column_database_name(_record.Handle, i).utf8_to_string();
schemaRow[BaseCatalogName] = databaseName;
schemaRow[baseCatalogNameColumn] = databaseName;
var columnName = sqlite3_column_origin_name(_record.Handle, i).utf8_to_string();
schemaRow[BaseColumnName] = columnName;
schemaRow[BaseSchemaName] = DBNull.Value;
schemaRow[baseColumnNameColumn] = columnName;
schemaRow[baseSchemaNameColumn] = DBNull.Value;
var tableName = sqlite3_column_table_name(_record.Handle, i).utf8_to_string();
schemaRow[BaseTableName] = tableName;
schemaRow[DataType] = GetFieldType(i);
schemaRow[baseTableNameColumn] = tableName;
schemaRow[dataTypeColumn] = GetFieldType(i);
var dataTypeName = GetDataTypeName(i);
schemaRow[DataTypeName] = dataTypeName;
schemaRow[IsAliased] = columnName != GetName(i);
schemaRow[IsExpression] = columnName == null;
schemaRow[IsLong] = DBNull.Value;
schemaRow[dataTypeNameColumn] = dataTypeName;
schemaRow[isAliasedColumn] = columnName != GetName(i);
schemaRow[isExpressionColumn] = columnName == null;
schemaRow[isLongColumn] = DBNull.Value;

var eponymousVirtualTable = false;
if (tableName != null
Expand All @@ -708,7 +707,7 @@ public override DataTable GetSchemaTable()
command.Parameters.AddWithValue("$column", columnName);

var cnt = (long)command.ExecuteScalar()!;
schemaRow[IsUnique] = cnt != 0;
schemaRow[isUniqueColumn] = cnt != 0;

command.Parameters.Clear();
var columnType = "typeof(\"" + columnName.Replace("\"", "\"\"") + "\")";
Expand All @@ -721,7 +720,7 @@ public override DataTable GetSchemaTable()
.AppendLine("LIMIT 1;").ToString();

var type = (string?)command.ExecuteScalar();
schemaRow[DataType] =
schemaRow[dataTypeColumn] =
(type != null)
? SqliteDataRecord.GetFieldType(type)
: SqliteDataRecord.GetFieldTypeFromSqliteType(
Expand All @@ -741,16 +740,29 @@ public override DataTable GetSchemaTable()
out var notNull, out var primaryKey, out var autoInc);
SqliteException.ThrowExceptionForRC(rc, _command.Connection.Handle);

schemaRow[IsKey] = primaryKey != 0;
schemaRow[AllowDBNull] = notNull == 0;
schemaRow[IsAutoIncrement] = autoInc != 0;
schemaRow[isKeyColumn] = primaryKey != 0;
schemaRow[allowDBNullColumn] = notNull == 0;
schemaRow[isAutoIncrementColumn] = autoInc != 0;
}
}

schemaTable.Rows.Add(schemaRow);
}

return schemaTable;

#if NET6_0_OR_GREATER
[UnconditionalSuppressMessage("Trimming", "IL2111:Method with parameters or return value with `DynamicallyAccessedMembersAttribute`"
+ " is accessed via reflection. Trimmer can't guarantee availability of the requirements of the method.",
Justification = "This is about System.Type.TypeInitializer.get. It is accessed via reflection"
+ " as the type parameter in DataColumn is annotated with DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.PublicProperties" +
" However, reflection is only used for nullable columns.")]
#endif
static DataColumn CreateDataTypeColumn()
=> new(SchemaTableColumn.DataType, typeof(Type))
{
AllowDBNull = false
};
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <auto-generated />
// <auto-generated />
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.NativeAotTests.Models;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <auto-generated />
// <auto-generated />
using Microsoft.EntityFrameworkCore.Metadata;

#pragma warning disable 219, 612, 618
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <auto-generated />
// <auto-generated />
using System.Reflection;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Microsoft.EntityFrameworkCore.ChangeTracking.Internal;
Expand Down
8 changes: 8 additions & 0 deletions test/EFCore.NativeAotTests/EFCore.NativeAotTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<OutputType>Exe</OutputType>
<TargetFramework>$(DefaultNetCoreTargetFramework)</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>true</ImplicitUsings>
<PublishAot>true</PublishAot>
<RootNamespace>Microsoft.EntityFrameworkCore.NativeAotTests</RootNamespace>
<AssemblyName>Microsoft.EntityFrameworkCore.NativeAotTests</AssemblyName>
Expand All @@ -12,6 +13,13 @@
<ItemGroup>
<ProjectReference Include="..\..\src\EFCore.Design\EFCore.Design.csproj" />
<ProjectReference Include="..\..\src\EFCore.SqlServer\EFCore.SqlServer.csproj" />
<ProjectReference Include="..\..\src\Microsoft.Data.Sqlite.Core\Microsoft.Data.Sqlite.Core.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="$(MicrosoftExtensionsConfigurationEnvironmentVariablesVersion)" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="$(MicrosoftExtensionsConfigurationJsonVersion)" />
<PackageReference Include="SQLitePCLRaw.bundle_e_sqlite3" Version="2.1.5" />
</ItemGroup>

</Project>
Loading

0 comments on commit d6fdc4d

Please sign in to comment.