@@ -44,28 +44,45 @@ public virtual void Apply(AssociationType item, DbModel model)
44
44
continue ;
45
45
}
46
46
47
+ string tableName = item . Constraint . ToRole . GetEntityType ( ) . GetTableName ( ) ;
48
+ string propertyName = edmProperty . Name ;
49
+
47
50
// The original attribute is removed. The none-ForeignKeyIndicies will be remained and readded without any modification
48
51
// and the foreignKeyIncidies will be readded with the correct name.
49
52
edmProperty . RemoveAnnotation ( IndexAnnotationName ) ;
50
53
51
54
// 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 ) ;
54
57
55
58
// 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 ++ )
59
61
{
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 ) ;
62
63
newIndexAnnotation = ( IndexAnnotation ) newIndexAnnotation . MergeWith ( foreignKeyIndexAnnotation ) ;
63
64
}
64
65
65
66
edmProperty . AddAnnotation ( IndexAnnotationName , newIndexAnnotation ) ;
66
67
}
67
68
}
68
69
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
+
69
86
private static IndexAnnotation GetAnnotation ( IEnumerable < MetadataProperty > metadataProperties , string name )
70
87
{
71
88
foreach ( MetadataProperty metadataProperty in metadataProperties )
0 commit comments