/// <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); } } }
/// <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); } }
private static void AddIndexColumn( string indexName, IndexOptions indexOptions, int column, PrimitivePropertyConfiguration propertyConfiguration) { var indexAttribute = new IndexAttribute(indexName, column) { IsClustered = indexOptions.HasFlag(IndexOptions.Clustered), IsUnique = indexOptions.HasFlag(IndexOptions.Unique) }; var annotation = GetIndexAnnotation(propertyConfiguration); if (annotation != null) { var attributes = annotation.Indexes.ToList(); attributes.Add(indexAttribute); annotation = new IndexAnnotation(attributes); } else { annotation = new IndexAnnotation(indexAttribute); } propertyConfiguration.HasColumnAnnotation(IndexAnnotation.AnnotationName, annotation); }
private static IndexAnnotation CreateIndexAnnotation(bool?isUnique = false, string name = null, int?order = null) { IndexAttribute indexAttribute; if (!string.IsNullOrWhiteSpace(name) && order.HasValue) { indexAttribute = new IndexAttribute(name, order.Value); } else if (!string.IsNullOrWhiteSpace(name)) { indexAttribute = new IndexAttribute(name); } else { indexAttribute = new IndexAttribute(); } if (isUnique.HasValue && isUnique.Value) { indexAttribute.IsUnique = true; } var indexAnnotation = new IndexAnnotation(indexAttribute); return(indexAnnotation); }
/// <summary> /// 将属性配置为唯一约束 /// </summary> /// <param name="property">属性</param> /// <returns></returns> public static PrimitivePropertyConfiguration HasUniqueIndexAnnotation( this PrimitivePropertyConfiguration property) { var indexAttribute = new IndexAttribute { IsUnique = true }; var indexAnnotation = new IndexAnnotation(indexAttribute); return(property.HasColumnAnnotation(IndexAnnotation.AnnotationName, indexAnnotation)); }
private IndexAnnotation DefinirChaveUnica(string nomeChave, int ordem) { var indexAttribute = new IndexAttribute(nomeChave) { IsUnique = true, Order = ordem }; var indexAnnotation = new IndexAnnotation(indexAttribute); return(indexAnnotation); }
public void WithIncludes() { var annotation = IndexAnnotation.AutoName("dbo.Part", "Company", "PartNum"); IndexDmlScriptCreator.GetScript(annotation).ToConsole(); var annotation2 = IndexAnnotation.AutoName("dbo.Part", "Company, PartNum", "IUM,PUM"); IndexDmlScriptCreator.GetScript(annotation2).ToConsole(); }
public void SimpleTest() { var annotation = IndexAnnotation.AutoName("dbo.Part", "Company"); IndexDmlScriptCreator.GetScript(annotation).ToConsole(); var annotation2 = IndexAnnotation.AutoName("dbo.Part", "Company, PartNum"); IndexDmlScriptCreator.GetScript(annotation2).ToConsole(); }
public static StringPropertyConfiguration AddColumnIndex(this StringPropertyConfiguration config, string indexName, int columnOrder, bool unique = false, bool clustered = false) { var indexAttribute = new IndexAttribute(indexName, columnOrder) { IsUnique = unique, IsClustered = clustered }; var indexAnnotation = new IndexAnnotation(indexAttribute); return(config.HasColumnAnnotation(IndexAnnotation.AnnotationName, indexAnnotation)); }
public static string GetScript(IndexAnnotation indexAnnotation) { var indexName = indexAnnotation.IndexName; var dbTableName = indexAnnotation.DbTableName; var indexColumnList = indexAnnotation.IndexColumns.Select(z => z.ToString()).StringJoin(", "); var script = @$ " CREATE INDEX {indexName} ON {dbTableName}({indexColumnList})--INCLUDE--;";
internal static PrimitivePropertyConfiguration HasIndex(this PrimitivePropertyConfiguration propertyConfiguration, string name, int order, bool isUnique = false, bool isClustered = false) { var indexAnnotation = new IndexAnnotation(new IndexAttribute(name) { Order = order, IsUnique = isUnique, IsClustered = isClustered, }); return(propertyConfiguration.HasColumnAnnotation("Index", indexAnnotation)); }
private static void AddAnnotationWithMerge(MetadataItem metadataItem, IndexAnnotation newAnnotation) { var existingAnnotation = metadataItem.Annotations.GetAnnotation(XmlConstants.IndexAnnotationWithPrefix); if (existingAnnotation != null) { newAnnotation = (IndexAnnotation)((IndexAnnotation)existingAnnotation).MergeWith(newAnnotation); } metadataItem.AddAnnotation(XmlConstants.IndexAnnotationWithPrefix, newAnnotation); }
public AdministratorMapping() { ToTable("VV_Administrators"); HasKey(a => a.Id); var usernameIndex = new IndexAnnotation(new IndexAttribute("uniqueUsername", 1) { IsUnique = true }); Property(a => a.UserName).HasColumnAnnotation("Index", usernameIndex).HasMaxLength(100); }
public static PrimitivePropertyConfiguration HasUniqueIndexAnnotation( this PrimitivePropertyConfiguration property, string indexName, int columnOrder = 0) { var indexAttribute = new IndexAttribute(indexName, columnOrder) { IsUnique = true }; var indexAnnotation = new IndexAnnotation(indexAttribute); return(property.HasColumnAnnotation("Index", indexAnnotation)); }
/// <summary> /// Creates a non-clustered /// </summary> /// <param name="property"></param> /// <param name="indexName">The index name.</param> /// <param name="columnOrder">A zero-based number which will be used to determine column ordering for multi-column indexes.</param> /// <param name="unique">Determines if unique constraint should be applied</param> /// <returns></returns> public static PrimitivePropertyConfiguration HasIndexAnnotation( this PrimitivePropertyConfiguration property, string indexName, int columnOrder, bool unique = false) { var indexAttribute = new IndexAttribute(indexName, columnOrder) { IsUnique = unique }; var indexAnnotation = new IndexAnnotation(indexAttribute); return(property.HasColumnAnnotation(IndexAnnotation.AnnotationName, indexAnnotation)); }
public LocationMapping() { ToTable("VV_Locations"); HasKey(a => a.Id); HasMany(l => l.Points).WithRequired(); var nameIndex = new IndexAnnotation(new IndexAttribute("uniqueName", 1) { IsUnique = true }); Property(a => a.Name).HasColumnAnnotation("Index", nameIndex).HasMaxLength(100); }
public AdvertiserMapping() { ToTable("VV_Advertisers"); HasKey(a => a.Id); var emailIndex = new IndexAnnotation(new IndexAttribute("uniqueEmail", 1) { IsUnique = true }); Property(a => a.Email).HasColumnAnnotation("Index", emailIndex).HasMaxLength(100); Ignore(a => a.UserName); }
public virtual void Apply(AssociationType item, DbModel model) { if (item == null) { throw new ArgumentNullException("item"); } if (model == null) { throw new ArgumentNullException("model"); } if (item.Constraint == null) { return; } foreach (var edmProperty in item.Constraint.ToProperties) { var annotation = GetAnnotation(edmProperty.MetadataProperties, IndexAnnotationName); if (annotation == null) { continue; } string tableName = item.Constraint.ToRole.GetEntityType().GetTableName(); // Special treatment for composite primary keys string propertyName = item.Constraint.FromProperties.Count > 1 ? String.Join("_", item.Constraint.ToProperties) : edmProperty.Name; // The original attribute is removed. The none-ForeignKeyIndicies will be remained and readded without any modification // and the foreignKeyIncidies will be readded with the correct name. edmProperty.RemoveAnnotation(IndexAnnotationName); // The schema for the automatically generated index name is "IX_{TableName}_{PropertyName}" var noneForeignKeyIndicies = annotation.Indexes.Where(index => index.Name != "IX_" + propertyName); var newIndexAnnotation = new IndexAnnotation(noneForeignKeyIndicies); // The schema for a FK index, which is generated by the Entity Framework, is "IX_{PropertyName}" var foreignKeyIndicies = annotation.Indexes.Where(index => index.Name == "IX_" + propertyName); for (int i = 0; i < foreignKeyIndicies.Count(); i++) { IndexAnnotation foreignKeyIndexAnnotation = CreateIndexAnnotation(tableName, propertyName, i); newIndexAnnotation = (IndexAnnotation)newIndexAnnotation.MergeWith(foreignKeyIndexAnnotation); } edmProperty.AddAnnotation(IndexAnnotationName, newIndexAnnotation); } }
public CategoriaConfiguration() { var indexAttribute = new IndexAttribute("CategoriaNomeUK") { IsUnique = true }; var indexAnnotation = new IndexAnnotation(indexAttribute); Property(p => p.Nome) .IsRequired() //.HasColumnName("") .HasMaxLength(15) .HasColumnType("nvarchar") .HasColumnAnnotation("Index", indexAnnotation); }
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); }
/// <summary> /// Applies an index to <paramref name="property" />. /// </summary> /// <remarks> /// Note: If the indexed property is of type string, you have to set the MaxLength. /// </remarks> /// <param name="property">The property instance.</param> /// <param name="indexName">The name of the database index. Default is "IX_Default" if not defined.</param> /// <param name="isUnique"> /// Set this property to true to define a unique index. Set this property to false to define a /// non-unique index. /// </param> /// <param name="columnOrder">The column order of the index. Default is 0 if not defined.</param> /// <returns></returns> public static PrimitivePropertyConfiguration HasIndex( this PrimitivePropertyConfiguration property, string indexName = "", bool isUnique = false, int columnOrder = 0) { if (string.IsNullOrEmpty(indexName)) { indexName = $"{IndexConstants.IndexPrefix}_{IndexConstants.DefaultIndexName}"; } var indexAttribute = new IndexAttribute(indexName, columnOrder) { IsUnique = isUnique }; var indexAnnotation = new IndexAnnotation(indexAttribute); return(property.HasColumnAnnotation(IndexAnnotation.AnnotationName, indexAnnotation)); }
protected override void OnModelCreating(DbModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); var userEntity = modelBuilder.Entity <User>(); userEntity.HasKey(x => x.Id); var emailUniqueIndex = new IndexAnnotation( new IndexAttribute("IX_Email") { IsUnique = true } ); userEntity.Property(x => x.EMail).IsRequired() .HasMaxLength(300) .HasColumnAnnotation(IndexAnnotation.AnnotationName, emailUniqueIndex); userEntity.Property(x => x.Name).IsRequired(); userEntity.Property(x => x.EMail).IsRequired(); userEntity.Property(x => x.Surname).IsRequired(); }
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()); }