/// <inheritdoc /> public virtual void Apply(AssociationType item, DbModel model) { Check.NotNull <AssociationType>(item, nameof(item)); if (item.Constraint == null) { return; } IEnumerable <ConsolidatedIndex> source = ConsolidatedIndex.BuildIndexes(item.Name, item.Constraint.ToProperties.Select <EdmProperty, Tuple <string, EdmProperty> >((Func <EdmProperty, Tuple <string, EdmProperty> >)(p => Tuple.Create <string, EdmProperty>(p.Name, p)))); IEnumerable <string> dependentColumnNames = item.Constraint.ToProperties.Select <EdmProperty, string>((Func <EdmProperty, string>)(p => p.Name)); if (source.Any <ConsolidatedIndex>((Func <ConsolidatedIndex, bool>)(c => c.Columns.SequenceEqual <string>(dependentColumnNames)))) { return; } string name = IndexOperation.BuildDefaultName(dependentColumnNames); int num = 0; foreach (EdmProperty toProperty in item.Constraint.ToProperties) { IndexAnnotation indexAnnotation = new IndexAnnotation(new IndexAttribute(name, num++)); object annotation = toProperty.Annotations.GetAnnotation("http://schemas.microsoft.com/ado/2013/11/edm/customannotation:Index"); if (annotation != null) { indexAnnotation = (IndexAnnotation)((IndexAnnotation)annotation).MergeWith((object)indexAnnotation); } toProperty.AddAnnotation("http://schemas.microsoft.com/ado/2013/11/edm/customannotation:Index", (object)indexAnnotation); } }
/// <inheritdoc /> public virtual void Apply(AssociationType item, DbModel model) { Check.NotNull(item, "item"); if (item.Constraint == null) { return; } var consolidatedIndexes = ConsolidatedIndex.BuildIndexes( item.Name, item.Constraint.ToProperties.Select(p => Tuple.Create(p.Name, p))); var dependentColumnNames = item.Constraint.ToProperties.Select(p => p.Name); if (!consolidatedIndexes.Any(c => c.Columns.SequenceEqual(dependentColumnNames))) { var name = IndexOperation.BuildDefaultName(dependentColumnNames); var order = 0; foreach (var dependentColumn in item.Constraint.ToProperties) { var newAnnotation = new IndexAnnotation(new IndexAttribute(name, order++)); var existingAnnotation = dependentColumn.Annotations.GetAnnotation(XmlConstants.IndexAnnotationWithPrefix); if (existingAnnotation != null) { newAnnotation = (IndexAnnotation)((IndexAnnotation)existingAnnotation).MergeWith(newAnnotation); } dependentColumn.AddAnnotation(XmlConstants.IndexAnnotationWithPrefix, newAnnotation); } } }
public void Apply_should_not_add_index_when_one_present() { var associationType = new AssociationType("A", "N", false, DataSpace.SSpace) { Constraint = new ReferentialConstraint( new AssociationEndMember("F", new EntityType("P", "N", DataSpace.SSpace)), new AssociationEndMember("T", new EntityType("D", "N", DataSpace.SSpace)), new EdmProperty[] { }, new[] { new EdmProperty("A"), new EdmProperty("B") }) }; (new ForeignKeyIndexConvention()).Apply(associationType, null); var consolidatedIndexes = ConsolidatedIndex.BuildIndexes( associationType.Name, associationType.Constraint.ToProperties.Select(p => Tuple.Create(p.Name, p))); Assert.Equal(1, consolidatedIndexes.Count()); (new ForeignKeyIndexConvention()).Apply(associationType, null); consolidatedIndexes = ConsolidatedIndex.BuildIndexes( associationType.Name, associationType.Constraint.ToProperties.Select(p => Tuple.Create(p.Name, p))); Assert.Equal(1, consolidatedIndexes.Count()); }