@@ -44,28 +44,45 @@ public virtual void Apply(AssociationType item, DbModel model)
4444 continue ;
4545 }
4646
47+ string tableName = item . Constraint . ToRole . GetEntityType ( ) . GetTableName ( ) ;
48+ string propertyName = edmProperty . Name ;
49+
4750 // The original attribute is removed. The none-ForeignKeyIndicies will be remained and readded without any modification
4851 // and the foreignKeyIncidies will be readded with the correct name.
4952 edmProperty . RemoveAnnotation ( IndexAnnotationName ) ;
5053
5154 // The schema for the automatically generated index name is "IX_{TableName}_{PropertyName}"
52- var noneForeignKeyIndicies = annotation . Indexes . Where ( index => index . Name != "IX_" + edmProperty . Name ) ;
53- IndexAnnotation newIndexAnnotation = new IndexAnnotation ( noneForeignKeyIndicies ) ;
55+ var noneForeignKeyIndicies = annotation . Indexes . Where ( index => index . Name != "IX_" + propertyName ) ;
56+ var newIndexAnnotation = new IndexAnnotation ( noneForeignKeyIndicies ) ;
5457
5558 // The schema for a FK index, which is generated by the Entity Framework, is "IX_{PropertyName}"
56- var property = edmProperty ;
57- var foreignKeyIndicies = annotation . Indexes . Where ( index => index . Name == "IX_" + property . Name ) ;
58- foreach ( var foreignKeyIndex in foreignKeyIndicies )
59+ var foreignKeyIndicies = annotation . Indexes . Where ( index => index . Name == "IX_" + propertyName ) ;
60+ for ( int i = 0 ; i < foreignKeyIndicies . Count ( ) ; i ++ )
5961 {
60- var indexAttribute = new IndexAttribute ( String . Format ( CultureInfo . InvariantCulture , "IX_{0}_{1}" , item . Constraint . ToRole . GetEntityType ( ) . GetTableName ( ) , edmProperty . Name ) ) ;
61- IndexAnnotation foreignKeyIndexAnnotation = new IndexAnnotation ( indexAttribute ) ;
62+ IndexAnnotation foreignKeyIndexAnnotation = CreateIndexAnnotation ( tableName , propertyName , i ) ;
6263 newIndexAnnotation = ( IndexAnnotation ) newIndexAnnotation . MergeWith ( foreignKeyIndexAnnotation ) ;
6364 }
6465
6566 edmProperty . AddAnnotation ( IndexAnnotationName , newIndexAnnotation ) ;
6667 }
6768 }
6869
70+ private static IndexAnnotation CreateIndexAnnotation ( string tableName , string propertyName , int count )
71+ {
72+ var indexName = String . Format ( CultureInfo . InvariantCulture , "IX_{0}_{1}" , tableName , propertyName ) ;
73+
74+ // If there are two Indicies on the same property, the count is added.
75+ // In SQLite an Index name must be global unique.
76+ // To be honest, it should never happen. But because its possible by using the API, it should be covered.
77+ if ( count > 0 )
78+ {
79+ indexName = String . Format ( "{0}_{1}" , indexName , count ) ;
80+ }
81+
82+ var indexAttribute = new IndexAttribute ( indexName ) ;
83+ return new IndexAnnotation ( indexAttribute ) ;
84+ }
85+
6986 private static IndexAnnotation GetAnnotation ( IEnumerable < MetadataProperty > metadataProperties , string name )
7087 {
7188 foreach ( MetadataProperty metadataProperty in metadataProperties )
0 commit comments