Skip to content

Commit

Permalink
SqlServerMigrationsAnnotationProvider: for indexes with included prop…
Browse files Browse the repository at this point in the history
…erties, resolve properties to column names.

Fixes #14087
  • Loading branch information
mirol-h authored and bricelam committed Aug 16, 2019
1 parent b315b56 commit 05f6c48
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,13 @@ public override IEnumerable<IAnnotation> For(IIndex index)
var includeProperties = index.GetIncludeProperties();
if (includeProperties != null)
{
var includeColumns = (IReadOnlyList<string>)includeProperties
.Select(p => index.DeclaringEntityType.FindProperty(p).GetColumnName())
.ToArray();

yield return new Annotation(
SqlServerAnnotationNames.Include,
includeProperties);
includeColumns);
}

var isOnline = index.IsCreatedOnline();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1455,15 +1455,15 @@ protected override void IndexTraits(MigrationOperation operation, IModel model,
/// <param name="builder"> The command builder to use to add the SQL fragment. </param>
protected override void IndexOptions(CreateIndexOperation operation, IModel model, MigrationCommandListBuilder builder)
{
if (operation[SqlServerAnnotationNames.Include] is IReadOnlyList<string> includeProperties
&& includeProperties.Count > 0)
if (operation[SqlServerAnnotationNames.Include] is IReadOnlyList<string> 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(", ");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Entity>().Property(e => e.IncludedProp).HasColumnName("IncludedColumn");
var index = _modelBuilder.Entity<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; }
}
}
}

0 comments on commit 05f6c48

Please sign in to comment.