diff --git a/src/EFCore.SqlServer/Migrations/Internal/SqlServerMigrationsAnnotationProvider.cs b/src/EFCore.SqlServer/Migrations/Internal/SqlServerMigrationsAnnotationProvider.cs index 8ae6f1ef835..d111de56f2f 100644 --- a/src/EFCore.SqlServer/Migrations/Internal/SqlServerMigrationsAnnotationProvider.cs +++ b/src/EFCore.SqlServer/Migrations/Internal/SqlServerMigrationsAnnotationProvider.cs @@ -89,9 +89,13 @@ public override IEnumerable For(IIndex index) var includeProperties = index.GetIncludeProperties(); if (includeProperties != null) { + var includeColumns = (IReadOnlyList)includeProperties + .Select(p => index.DeclaringEntityType.FindProperty(p).GetColumnName()) + .ToArray(); + yield return new Annotation( SqlServerAnnotationNames.Include, - includeProperties); + includeColumns); } var isOnline = index.IsCreatedOnline(); diff --git a/src/EFCore.SqlServer/Migrations/SqlServerMigrationsSqlGenerator.cs b/src/EFCore.SqlServer/Migrations/SqlServerMigrationsSqlGenerator.cs index 3aa4c5b313b..507953a1688 100644 --- a/src/EFCore.SqlServer/Migrations/SqlServerMigrationsSqlGenerator.cs +++ b/src/EFCore.SqlServer/Migrations/SqlServerMigrationsSqlGenerator.cs @@ -1455,15 +1455,15 @@ protected override void IndexTraits(MigrationOperation operation, IModel model, /// The command builder to use to add the SQL fragment. protected override void IndexOptions(CreateIndexOperation operation, IModel model, MigrationCommandListBuilder builder) { - if (operation[SqlServerAnnotationNames.Include] is IReadOnlyList includeProperties - && includeProperties.Count > 0) + if (operation[SqlServerAnnotationNames.Include] is IReadOnlyList includeColumns + && includeColumns.Count > 0) { builder.Append(" INCLUDE ("); - for (var i = 0; i < includeProperties.Count; i++) + for (var i = 0; i < includeColumns.Count; i++) { - builder.Append(Dependencies.SqlGenerationHelper.DelimitIdentifier(includeProperties[i])); + builder.Append(Dependencies.SqlGenerationHelper.DelimitIdentifier(includeColumns[i])); - if (i != includeProperties.Count - 1) + if (i != includeColumns.Count - 1) { builder.Append(", "); } diff --git a/test/EFCore.SqlServer.Tests/Migrations/SqlServerMigrationsAnnotationProviderTest.cs b/test/EFCore.SqlServer.Tests/Migrations/SqlServerMigrationsAnnotationProviderTest.cs index 10927562a7f..36ee2fd9f51 100644 --- a/test/EFCore.SqlServer.Tests/Migrations/SqlServerMigrationsAnnotationProviderTest.cs +++ b/test/EFCore.SqlServer.Tests/Migrations/SqlServerMigrationsAnnotationProviderTest.cs @@ -32,5 +32,22 @@ public void For_property_handles_identity_annotations() var identity = Assert.Single(migrationAnnotations, a => a.Name == SqlServerAnnotationNames.Identity); Assert.Equal("2, 3", identity.Value); } + + [ConditionalFact] + public void Resolves_column_names_for_Index_with_included_properties() + { + _modelBuilder.Entity().Property(e => e.IncludedProp).HasColumnName("IncludedColumn"); + var index = _modelBuilder.Entity().HasIndex(e => e.IndexedProp).IncludeProperties(e => e.IncludedProp).Metadata; + _modelBuilder.FinalizeModel(); + + Assert.Contains(_annotations.For(index), a => a.Name == SqlServerAnnotationNames.Include && ((string[])a.Value).Contains("IncludedColumn")); + } + + private class Entity + { + public int Id { get; set; } + public string IndexedProp { get; set; } + public string IncludedProp { get; set; } + } } }