public void Multiple_attributes_with_a_given_name_are_merged_when_IndexAnnotation_is_constructed() { var annotation = new IndexAnnotation( new[] { new IndexAttribute(), new IndexAttribute("EekyBear"), new IndexAttribute { Order = 1 }, new IndexAttribute("EekyBear") { Order = 0 }, new IndexAttribute { IsClustered = true, IsUnique = false }, new IndexAttribute("EekyBear") { Order = 0, IsClustered = false, IsUnique = true } }); var attributes = annotation.Indexes.ToArray(); Assert.Equal(2, attributes.Length); Assert.Null(attributes[0].Name); Assert.Equal(1, attributes[0].Order); Assert.True(attributes[0].IsClusteredConfigured); Assert.True(attributes[0].IsClustered); Assert.True(attributes[0].IsUniqueConfigured); Assert.False(attributes[0].IsUnique); Assert.Equal("EekyBear", attributes[1].Name); Assert.Equal(0, attributes[1].Order); Assert.True(attributes[1].IsClusteredConfigured); Assert.False(attributes[1].IsClustered); Assert.True(attributes[1].IsUniqueConfigured); Assert.True(attributes[1].IsUnique); }
public void MergeWith_returns_current_instance_if_other_is_same_or_null() { var annotation = new IndexAnnotation(new IndexAttribute()); Assert.Same(annotation, annotation.MergeWith(annotation)); Assert.Same(annotation, annotation.MergeWith(null)); }
/// <summary> /// Returns true if this annotation does not conflict with the given annotation such that /// the two can be combined together using the <see cref="M:System.Data.Entity.Infrastructure.Annotations.IndexAnnotation.MergeWith(System.Object)" /> method. /// </summary> /// <remarks> /// Each index annotation contains at most one <see cref="T:System.ComponentModel.DataAnnotations.Schema.IndexAttribute" /> with a given name. /// Two annotations are considered compatible if each IndexAttribute with a given name is only /// contained in one annotation or the other, or if both annotations contain an IndexAttribute /// with the given name. /// </remarks> /// <param name="other">The annotation to compare.</param> /// <returns>A CompatibilityResult indicating whether or not this annotation is compatible with the other.</returns> public virtual CompatibilityResult IsCompatibleWith(object other) { if (object.ReferenceEquals((object)this, other) || other == null) { return(new CompatibilityResult(true, (string)null)); } IndexAnnotation indexAnnotation = other as IndexAnnotation; if (indexAnnotation == null) { return(new CompatibilityResult(false, Strings.IncompatibleTypes((object)other.GetType().Name, (object)typeof(IndexAnnotation).Name))); } foreach (IndexAttribute index in (IEnumerable <IndexAttribute>)indexAnnotation._indexes) { IndexAttribute newIndex = index; IndexAttribute me = this._indexes.SingleOrDefault <IndexAttribute>((Func <IndexAttribute, bool>)(i => i.Name == newIndex.Name)); if (me != null) { CompatibilityResult compatibilityResult = me.IsCompatibleWith(newIndex, false); if (!(bool)compatibilityResult) { return(compatibilityResult); } } } return(new CompatibilityResult(true, (string)null)); }
public void IsCompatibleWith_returns_true_if_contained_index_lists_are_compatible() { var annotation1 = new IndexAnnotation( new[] { new IndexAttribute(), new IndexAttribute("EekyBear"), new IndexAttribute("EekyBear") { Order = 0, IsClustered = false, IsUnique = true } }); var annotation2 = new IndexAnnotation( new[] { new IndexAttribute { Order = 1 }, new IndexAttribute("EekyBear") { Order = 0 }, new IndexAttribute { IsClustered = true, IsUnique = false }, }); Assert.True(annotation1.IsCompatibleWith(annotation2)); Assert.True(annotation2.IsCompatibleWith(annotation1)); }
public void IsCompatibleWith_returns_true_if_other_is_same_or_null() { var annotation = new IndexAnnotation(new IndexAttribute()); Assert.True(annotation.IsCompatibleWith(annotation)); Assert.True(annotation.IsCompatibleWith(null)); }
public void MergeWith_throws_if_other_is_not_an_IndexAnnotation() { var annotation = new IndexAnnotation(new IndexAttribute()); Assert.Equal( Strings.IncompatibleTypes("Random", "IndexAnnotation"), Assert.Throws <ArgumentException>(() => annotation.MergeWith(new Random())).Message); }
public void IsCompatibleWith_returns_false_if_other_is_not_an_IndexAnnotation() { var annotation = new IndexAnnotation(new IndexAttribute()); var result = annotation.IsCompatibleWith(new Random()); Assert.False(result); Assert.Equal(Strings.IncompatibleTypes("Random", "IndexAnnotation"), result.ErrorMessage); }
/// <summary> /// Merges this annotation with the given annotation and returns a new annotation containing the merged indexes. /// </summary> /// <remarks> /// Each index annotation contains at most one <see cref="T:System.ComponentModel.DataAnnotations.Schema.IndexAttribute" /> with a given name. /// The merged annotation will contain IndexAttributes from both this and the other annotation. /// If both annotations contain an IndexAttribute with the same name, then the merged annotation /// will contain one IndexAttribute with that name. /// </remarks> /// <param name="other">The annotation to merge with this one.</param> /// <returns>A new annotation with indexes from both annotations merged.</returns> /// <exception cref="T:System.InvalidOperationException"> /// The other annotation contains indexes that are not compatible with indexes in this annotation. /// </exception> public virtual object MergeWith(object other) { if (object.ReferenceEquals((object)this, other) || other == null) { return((object)this); } IndexAnnotation indexAnnotation = other as IndexAnnotation; if (indexAnnotation == null) { throw new ArgumentException(Strings.IncompatibleTypes((object)other.GetType().Name, (object)typeof(IndexAnnotation).Name)); } List <IndexAttribute> list = this._indexes.ToList <IndexAttribute>(); IndexAnnotation.MergeLists((ICollection <IndexAttribute>)list, (IEnumerable <IndexAttribute>)indexAnnotation._indexes, (PropertyInfo)null); return((object)new IndexAnnotation((IEnumerable <IndexAttribute>)list)); }
/// <summary> /// Serializes the given <see cref="T:System.Data.Entity.Infrastructure.Annotations.IndexAnnotation" /> into a string for storage in the EDMX XML. /// </summary> /// <param name="name">The name of the annotation that is being serialized.</param> /// <param name="value">The value to serialize which must be an IndexAnnotation object.</param> /// <returns>The serialized value.</returns> public virtual string Serialize(string name, object value) { Check.NotEmpty(name, nameof(name)); Check.NotNull <object>(value, nameof(value)); IndexAnnotation indexAnnotation = value as IndexAnnotation; if (indexAnnotation == null) { throw new ArgumentException(Strings.AnnotationSerializeWrongType((object)value.GetType().Name, (object)typeof(IndexAnnotationSerializer).Name, (object)typeof(IndexAnnotation).Name)); } StringBuilder stringBuilder = new StringBuilder(); foreach (IndexAttribute index in indexAnnotation.Indexes) { stringBuilder.Append(IndexAnnotationSerializer.SerializeIndexAttribute(index)); } return(stringBuilder.ToString()); }
public void Can_roundtrip_index_names_having_special_characters() { Assert.Equal( "{ Name: \"\\,'<>[]\\,'\\} }{ Name: \"\\,'<foo>[]\\,'\\} }", Serialize(Deserialize("{Name:\"\\,'<>[]\\,'\\}}{Name:\"\\,'<foo>[]\\,'\\}}"))); Assert.Equal( "{ Name: \"\\,'<>[]\\,'\\}, Order: 42 }{ Name: \"\\,'<foo>[]\\,'\\}, Order: 42 }", Serialize(Deserialize("{Name:\"\\,'<>[]\\,'\\},Order: 42} { Name: \"\\,'<foo>[]\\,'\\}, Order: 42 }"))); var indexAnnotation1 = new IndexAnnotation(new IndexAttribute("\",'<>[],'")); var indexAnnotation2 = Deserialize(Serialize(indexAnnotation1)); Assert.NotSame(indexAnnotation1, indexAnnotation2); Assert.Equal("\",'<>[],'", indexAnnotation2.Indexes.Single().Name); indexAnnotation1 = new IndexAnnotation(new IndexAttribute("\",'<>[],'", 42)); indexAnnotation2 = Deserialize(Serialize(indexAnnotation1)); Assert.NotSame(indexAnnotation1, indexAnnotation2); Assert.Equal("\",'<>[],'", indexAnnotation2.Indexes.Single().Name); Assert.Equal(42, indexAnnotation2.Indexes.Single().Order); indexAnnotation1 = new IndexAnnotation(new IndexAttribute("\",'<>[]',")); indexAnnotation2 = Deserialize(Serialize(indexAnnotation1)); Assert.NotSame(indexAnnotation1, indexAnnotation2); Assert.Equal("\",'<>[]',", indexAnnotation2.Indexes.Single().Name); indexAnnotation1 = new IndexAnnotation(new IndexAttribute("\",'<>[]',", 42)); indexAnnotation2 = Deserialize(Serialize(indexAnnotation1)); Assert.NotSame(indexAnnotation1, indexAnnotation2); Assert.Equal("\",'<>[]',", indexAnnotation2.Indexes.Single().Name); Assert.Equal(42, indexAnnotation2.Indexes.Single().Order); }
public void MergeLists_throws_if_lists_are_not_compatible() { var annotation1 = new IndexAnnotation( new[] { new IndexAttribute(), new IndexAttribute("EekyBear"), new IndexAttribute("EekyBear") { Order = 0, IsClustered = false, IsUnique = true } }); var annotation2 = new IndexAnnotation( new[] { new IndexAttribute { Order = 1 }, new IndexAttribute("EekyBear") { Order = 1 }, new IndexAttribute { IsClustered = true, IsUnique = false }, }); Assert.Equal( Strings.ConflictingIndexAttribute( "EekyBear", Environment.NewLine + "\t" + Strings.ConflictingIndexAttributeProperty("Order", "1", "0")), Assert.Throws <InvalidOperationException>( () => annotation1.MergeWith(annotation2)).Message); Assert.Equal( Strings.ConflictingIndexAttribute( "EekyBear", Environment.NewLine + "\t" + Strings.ConflictingIndexAttributeProperty("Order", "0", "1")), Assert.Throws <InvalidOperationException>( () => annotation2.MergeWith(annotation1)).Message); }
public void IsCompatibleWith_returns_false_if_any_contained_indexes_are_not_compatible() { var annotation1 = new IndexAnnotation( new[] { new IndexAttribute(), new IndexAttribute("EekyBear"), new IndexAttribute("EekyBear") { Order = 0, IsClustered = false, IsUnique = true } }); var annotation2 = new IndexAnnotation( new[] { new IndexAttribute { Order = 1 }, new IndexAttribute("EekyBear") { Order = 1 }, new IndexAttribute { IsClustered = true, IsUnique = false }, }); var result = annotation1.IsCompatibleWith(annotation2); Assert.False(result); Assert.Equal(Strings.ConflictingIndexAttributeProperty("Order", "0", "1"), result.ErrorMessage); result = annotation2.IsCompatibleWith(annotation1); Assert.False(result); Assert.Equal(Strings.ConflictingIndexAttributeProperty("Order", "1", "0"), result.ErrorMessage); }
public static void BuildModel(DbModelBuilder modelBuilder) { ConfigureConventions(modelBuilder); ConfigureRelations(modelBuilder); ConfigureConstraints(modelBuilder); EntityTypeConfiguration<User> userConfiguration = modelBuilder.Entity<User>().ToTable("User"); userConfiguration.HasMany(u => u.UserRoles).WithRequired().HasForeignKey(role => role.UserId); userConfiguration.HasMany(u => u.Claims).WithRequired().HasForeignKey(claim => claim.UserId); userConfiguration.HasMany(u => u.Logins).WithRequired().HasForeignKey(login => login.UserId); StringPropertyConfiguration propertyUserNameConfiguration = userConfiguration.Property(u => u.UserName).IsRequired().HasMaxLength(256); string annotationUserName = "******"; IndexAttribute annotationUserNameIndexAttribute = new IndexAttribute("UserNameIndex") { IsUnique = true }; IndexAnnotation annotationUserNameIndex = new IndexAnnotation(annotationUserNameIndexAttribute); propertyUserNameConfiguration.HasColumnAnnotation(annotationUserName, annotationUserNameIndex); userConfiguration.Property(u => u.Email).HasMaxLength(256); modelBuilder.Entity<UserRole>().HasKey(role => new { role.UserId, role.RoleId }); modelBuilder.Entity<UserRole>().ToTable("UsersRoles"); modelBuilder.Entity<UserLogin>().HasKey(login => new { login.LoginProvider, login.ProviderKey, login.UserId }); modelBuilder.Entity<UserLogin>().ToTable("UserLogin"); modelBuilder.Entity<UserClaim>().ToTable("UserClaim"); EntityTypeConfiguration<Role> roleConfiguration = modelBuilder.Entity<Role>().ToTable("Role"); StringPropertyConfiguration propertyNameConfiguration = roleConfiguration.Property(r => r.Name).IsRequired().HasMaxLength(256); string annotationName = "Index"; IndexAttribute annotationNameIndexAttribute = new IndexAttribute("RoleNameIndex") { IsUnique = true }; IndexAnnotation annotationNameIndex = new IndexAnnotation(annotationNameIndexAttribute); propertyNameConfiguration.HasColumnAnnotation(annotationName, annotationNameIndex); roleConfiguration.HasMany(role => role.UserRoles).WithRequired().HasForeignKey(userRole => userRole.RoleId); }
private static void CheckIndex( IEnumerable<EntityType> entityTypes, string storeTypeName, string columnName, IndexAnnotation expected) { var annotation = entityTypes.Single(e => e.Name == storeTypeName) .Properties.Single(p => p.Name == columnName) .MetadataProperties.Single(p => p.Name == CustomAnnotationNamespace + ":Index") .Value; Assert.IsType<IndexAnnotation>(annotation); Assert.Equal(expected, annotation, new IndexAnnotationEqualityComparer()); }
private static string Serialize(IndexAnnotation annotation) { return(new IndexAnnotationSerializer().Serialize("Index", annotation)); }
private static string Serialize(IndexAnnotation annotation) { return new IndexAnnotationSerializer().Serialize("Index", annotation); }
public void MergeLists_throws_if_lists_are_not_compatible() { var annotation1 = new IndexAnnotation( new[] { new IndexAttribute(), new IndexAttribute("EekyBear"), new IndexAttribute("EekyBear") { Order = 0, IsClustered = false, IsUnique = true } }); var annotation2 = new IndexAnnotation( new[] { new IndexAttribute { Order = 1 }, new IndexAttribute("EekyBear") { Order = 1 }, new IndexAttribute { IsClustered = true, IsUnique = false }, }); Assert.Equal( Strings.ConflictingIndexAttribute( "EekyBear", Environment.NewLine + "\t" + Strings.ConflictingIndexAttributeProperty("Order", "1", "0")), Assert.Throws<InvalidOperationException>( () => annotation1.MergeWith(annotation2)).Message); Assert.Equal( Strings.ConflictingIndexAttribute( "EekyBear", Environment.NewLine + "\t" + Strings.ConflictingIndexAttributeProperty("Order", "0", "1")), Assert.Throws<InvalidOperationException>( () => annotation2.MergeWith(annotation1)).Message); }
public void MergeWith_merges_index_lists_from_both_annotations() { var annotation1 = new IndexAnnotation( new[] { new IndexAttribute(), new IndexAttribute("EekyBear"), new IndexAttribute("EekyBear") { Order = 0, IsClustered = false, IsUnique = true } }); var annotation2 = new IndexAnnotation( new[] { new IndexAttribute { Order = 1 }, new IndexAttribute("EekyBear") { Order = 0 }, new IndexAttribute { IsClustered = true, IsUnique = false }, }); var attributes = ((IndexAnnotation)annotation1.MergeWith(annotation2)).Indexes.ToArray(); Assert.Equal(2, attributes.Length); Assert.Equal("EekyBear", attributes[0].Name); Assert.Equal(0, attributes[0].Order); Assert.True(attributes[0].IsClusteredConfigured); Assert.False(attributes[0].IsClustered); Assert.True(attributes[0].IsUniqueConfigured); Assert.True(attributes[0].IsUnique); Assert.Null(attributes[1].Name); Assert.Equal(1, attributes[1].Order); Assert.True(attributes[1].IsClusteredConfigured); Assert.True(attributes[1].IsClustered); Assert.True(attributes[1].IsUniqueConfigured); Assert.False(attributes[1].IsUnique); attributes = ((IndexAnnotation)annotation2.MergeWith(annotation1)).Indexes.ToArray(); Assert.Equal(2, attributes.Length); Assert.Null(attributes[0].Name); Assert.Equal(1, attributes[0].Order); Assert.True(attributes[0].IsClusteredConfigured); Assert.True(attributes[0].IsClustered); Assert.True(attributes[0].IsUniqueConfigured); Assert.False(attributes[0].IsUnique); Assert.Equal("EekyBear", attributes[1].Name); Assert.Equal(0, attributes[1].Order); Assert.True(attributes[1].IsClusteredConfigured); Assert.False(attributes[1].IsClustered); Assert.True(attributes[1].IsUniqueConfigured); Assert.True(attributes[1].IsUnique); }
public void MergeWith_throws_if_other_is_not_an_IndexAnnotation() { var annotation = new IndexAnnotation(new IndexAttribute()); Assert.Equal( Strings.IncompatibleTypes("Random", "IndexAnnotation"), Assert.Throws<ArgumentException>(() => annotation.MergeWith(new Random())).Message); }
/// <summary> /// Creates a new annotation for the given collection of indexes. /// </summary> /// <param name="indexAttributes">Index attributes representing one or more indexes.</param> public IndexAnnotation(IEnumerable <IndexAttribute> indexAttributes) { Check.NotNull <IEnumerable <IndexAttribute> >(indexAttributes, nameof(indexAttributes)); IndexAnnotation.MergeLists((ICollection <IndexAttribute>) this._indexes, indexAttributes, (PropertyInfo)null); }
internal IndexAnnotation(PropertyInfo propertyInfo, IEnumerable <IndexAttribute> indexAttributes) { Check.NotNull <IEnumerable <IndexAttribute> >(indexAttributes, nameof(indexAttributes)); IndexAnnotation.MergeLists((ICollection <IndexAttribute>) this._indexes, indexAttributes, propertyInfo); }