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

Add support for defining included column for indexes in SQL server #12266

Merged
merged 1 commit into from
Jun 14, 2018
Merged

Add support for defining included column for indexes in SQL server #12266

merged 1 commit into from
Jun 14, 2018

Conversation

Kukkimonsuta
Copy link
Contributor

This PR adds ForSqlServerInclude extension to IndexBuilder allowing defining columns to be included in a index on sql server.

To support expression parameter a generic version of IndexBuilder is introduced in main EF assembly, however to avoid breaking changes (as requested in #4846 (comment)) this type is available only with new extension ForSqlServerHasIndex for the time being.

There is a breaking change in EFCore.SqlServer adding a property to interface -
ISqlServerIndexAnnotations.Include, which might be ok since it's maybe not really meant to be implemented by others? @ajcvickers

I'm still investigating possible problems with migrations, so this is not yet ready.

Fixes #4846

@dnfclas
Copy link

dnfclas commented Jun 6, 2018

CLA assistant check
All CLA requirements met.

@Kukkimonsuta
Copy link
Contributor Author

I've used string[] as value of annotation which seem to be unsupported by the differ. I've worked around it by adding special case for any IEnumerable<string>, but I'm not sure whether it's the right solution.

  • Is supporting collections as annotation values acceptable?
  • Is special casing for IEnumerable<string> enough or should it be more general IEnumerable<object> or even more strict string[]?

@Kukkimonsuta
Copy link
Contributor Author

Rebased onto latest dev.

One thing I think is worth noting is that property.GetContainingIndexes will not return indexes that have this property in include clause. I think it makes sense and sql generator
works around this by scanning whole entity instead ( f236b8c#diff-42efb7d65f79f8f8347d8753fcee5498R1683 ), but it's certainly possible it may cause trouble elsewhere.

Other than that I have not found any further issues, so this should be ready for review.

@Kukkimonsuta Kukkimonsuta changed the title [WIP] Add support for defining included column for indexes in SQL server Add support for defining included column for indexes in SQL server Jun 8, 2018
@@ -1896,7 +1896,7 @@ protected virtual bool HasDifferences([NotNull] IEnumerable<IAnnotation> source,

foreach (var annotation in source)
{
var index = unmatched.FindIndex(a => a.Name == annotation.Name && Equals(a.Value, annotation.Value));
var index = unmatched.FindIndex(a => a.Name == annotation.Name && AnnotationValueEquals(a.Value, annotation.Value));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just call StructuralComparisons.StructuralEqualityComparer.Equals

Copy link
Member

@AndriySvyryd AndriySvyryd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also add a rule to SqlServerModelValidator that throws if any of the included properties are not present on the entity type or are already part of the index

@Kukkimonsuta
Copy link
Contributor Author

Kukkimonsuta commented Jun 14, 2018

  1. added generic variants of HasAnnotation, IsUnique, HasName, HasFilter, ForSqlServerIsClustered to index builder
  2. changed differ to use structural comparison for annotations
  3. renamed Include to IncludeProperties (I've used includeProperties everywhere for consitency)
  4. add validation of duplicate/missing/already included properties

As we stated in #4846 (comment) this method shouldn't change. Instead ForSqlServerHasIndex extension method should be added.

This was addressed here f947625

Let me know if I should rebase/squash changes

Copy link
Member

@AndriySvyryd AndriySvyryd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Please rebase and squash.

This commit adds `ForSqlServerInclude` extension to `IndexBuilder` allowing defining columns to be included in a index on sql server.

Generic version of `IndexBuilder` together with its extensions is introduced to support expression parameter.

Breaking change is introduced in `SqlServer` adding a property to interface - `ISqlServerIndexAnnotations.IncludeProperties`.

`MigrationsModelDiffer` now uses structural comparison for annotations to support arrays as annotation value.

Fixes #4846
@Kukkimonsuta
Copy link
Contributor Author

Rebased and squashed.

@AndriySvyryd AndriySvyryd merged commit a3dd2a0 into dotnet:dev Jun 14, 2018
@AndriySvyryd
Copy link
Member

@Kukkimonsuta Thanks for your contribution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants