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

Fix an issue when a property is configured in multiple indexes #534

Closed
wants to merge 2 commits into from
Closed
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 @@ -92,16 +92,54 @@ internal void Configure(EdmProperty edmProperty, int indexOrder)
{
DebugCheck.NotNull(edmProperty);

edmProperty.AddAnnotation(XmlConstants.IndexAnnotationWithPrefix,
new IndexAnnotation(new IndexAttribute(_name, indexOrder, _isClustered, _isUnique)));
var annotation = edmProperty.Annotations.FirstOrDefault(x => x.Name == XmlConstants.IndexAnnotationWithPrefix);

var attribute = new IndexAttribute(_name, indexOrder, _isClustered, _isUnique);

if (annotation == null)
{
edmProperty.AddAnnotation(
XmlConstants.IndexAnnotationWithPrefix,
new IndexAnnotation(attribute));
}
else
{
var indexAnnotation = annotation.Value as IndexAnnotation;

if (indexAnnotation != null)
{
edmProperty.AddAnnotation(
XmlConstants.IndexAnnotationWithPrefix,
new IndexAnnotation(indexAnnotation.Indexes.Concat(new[] { attribute })));
}
}
}

internal void Configure(EntityType entityType)
{
DebugCheck.NotNull(entityType);

entityType.AddAnnotation(XmlConstants.IndexAnnotationWithPrefix,
new IndexAnnotation(new IndexAttribute(_name, _isClustered, _isUnique)));
var annotation = entityType.Annotations.FirstOrDefault(x => x.Name == XmlConstants.IndexAnnotationWithPrefix);

var attribute = new IndexAttribute(_name, _isClustered, _isUnique);

if (annotation == null)
{
entityType.AddAnnotation(
XmlConstants.IndexAnnotationWithPrefix,
new IndexAnnotation(attribute));
}
else
{
var indexAnnotation = annotation.Value as IndexAnnotation;

if (indexAnnotation != null)
{
entityType.AddAnnotation(
XmlConstants.IndexAnnotationWithPrefix,
new IndexAnnotation(indexAnnotation.Indexes.Concat(new[] { attribute })));
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace System.Data.Entity.ModelConfiguration
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Core.Metadata.Edm;
using System.Data.Entity.Infrastructure.Annotations;
using System.Data.Entity.ModelConfiguration.Configuration;
using System.Data.Entity.ModelConfiguration.Configuration.Mapping;
using System.Data.Entity.ModelConfiguration.Configuration.Properties.Navigation;
Expand Down Expand Up @@ -1053,5 +1054,65 @@ public void Build_adds_the_UseClrTypes_annotation_to_the_container()
databaseMapping.Model.Container.Annotations.Count(
a => a.Name == XmlConstants.UseClrTypesAnnotationWithPrefix && a.Value.Equals("true")));
}

[Fact]
public void Registering_two_indexes_with_a_shared_property_puts_two_index_attributes_in_the_index_annotation()
{
var builder = new DbModelBuilder();

var entityTypeConfiguration = builder.Entity<TestType>();

entityTypeConfiguration
.HasIndex(x => new { x.Property1, x.Property2 });

entityTypeConfiguration
.HasIndex(x => new { x.Property2, x.Property3 });

var model = builder.Build(ProviderRegistry.Sql2008_ProviderInfo);

var mapping = model.DatabaseMapping;

var database = mapping.Database;

var entity = database.EntityTypes.Single(x => x.Name == nameof (TestType));

var property1 = entity.DeclaredProperties.Single(x => x.Name == nameof (TestType.Property1));
var property2 = entity.DeclaredProperties.Single(x => x.Name == nameof (TestType.Property2));
var property3 = entity.DeclaredProperties.Single(x => x.Name == nameof (TestType.Property3));

var property1Attributes = property1.Annotations
.Where(x => x.Name == XmlConstants.IndexAnnotationWithPrefix)
.Select(x => x.Value)
.Cast<IndexAnnotation>()
.SelectMany(x => x.Indexes)
.ToList();

var property2Attributes = property2.Annotations
.Where(x => x.Name == XmlConstants.IndexAnnotationWithPrefix)
.Select(x => x.Value)
.Cast<IndexAnnotation>()
.SelectMany(x => x.Indexes)
.ToList();

var property3Attributes = property3.Annotations
.Where(x => x.Name == XmlConstants.IndexAnnotationWithPrefix)
.Select(x => x.Value)
.Cast<IndexAnnotation>()
.SelectMany(x => x.Indexes)
.ToList();

Assert.Equal(property1Attributes.Count, 1);
Assert.Equal(property2Attributes.Count, 2);
Assert.Equal(property3Attributes.Count, 1);
}
}

public class TestType
{
public int Id { get; set; }

public int Property1 { get; set; }
public int Property2 { get; set; }
public int Property3 { get; set; }
}
}