Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SqlServer: resolve included properties of index to column names (in migrations) #16960

Merged
merged 1 commit into from
Aug 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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; }
}
}
}