|
6 | 6 | using System.Data.Entity.Infrastructure; |
7 | 7 | using System.Data.Entity.Infrastructure.Annotations; |
8 | 8 | using System.Data.Entity.ModelConfiguration.Conventions; |
| 9 | +using System.Globalization; |
9 | 10 | using System.Linq; |
10 | 11 |
|
11 | 12 | namespace SQLite.CodeFirst.Convention |
@@ -35,31 +36,33 @@ public virtual void Apply(AssociationType item, DbModel model) |
35 | 36 | return; |
36 | 37 | } |
37 | 38 |
|
38 | | - for (int i = 0; i < item.Constraint.ToProperties.Count; i++) |
| 39 | + foreach (var edmProperty in item.Constraint.ToProperties) |
39 | 40 | { |
40 | | - EdmProperty edmProperty = item.Constraint.ToProperties[i]; |
41 | 41 | var annotation = GetAnnotation(edmProperty.MetadataProperties, IndexAnnotationName); |
42 | | - if (annotation != null) |
| 42 | + if (annotation == null) |
43 | 43 | { |
44 | | - // The original attribute is removed. The none-ForeignKeyIndicies will be remained and readded without any modification |
45 | | - // and the foreignKeyIncidies will be readded with the correct name. |
46 | | - edmProperty.RemoveAnnotation(IndexAnnotationName); |
| 44 | + continue; |
| 45 | + } |
47 | 46 |
|
48 | | - // The schema for the automatically generated index name is "IX_{TableName}_{PropertyName}" |
49 | | - var noneForeignKeyIndicies = annotation.Indexes.Where(index => index.Name != "IX_" + edmProperty.Name); |
50 | | - IndexAnnotation newIndexAnnotation = new IndexAnnotation(noneForeignKeyIndicies); |
| 47 | + // The original attribute is removed. The none-ForeignKeyIndicies will be remained and readded without any modification |
| 48 | + // and the foreignKeyIncidies will be readded with the correct name. |
| 49 | + edmProperty.RemoveAnnotation(IndexAnnotationName); |
51 | 50 |
|
52 | | - // The schema for a FK index, which is generated by the Entity Framework, is "IX_{PropertyName}" |
53 | | - var foreignKeyIndicies = annotation.Indexes.Where(index => index.Name == "IX_" + edmProperty.Name); |
54 | | - foreach (var foreignKeyIndex in foreignKeyIndicies) |
55 | | - { |
56 | | - var indexAttribute = new IndexAttribute(string.Format("IX_{0}_{1}", item.Constraint.ToRole.GetEntityType().GetTableName(), edmProperty.Name)); |
57 | | - IndexAnnotation foreignKeyIndexAnnotation = new IndexAnnotation(indexAttribute); |
58 | | - newIndexAnnotation = (IndexAnnotation)newIndexAnnotation.MergeWith(foreignKeyIndexAnnotation); |
59 | | - } |
| 51 | + // 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); |
60 | 54 |
|
61 | | - edmProperty.AddAnnotation(IndexAnnotationName, newIndexAnnotation); |
| 55 | + // 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 | + { |
| 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 | + newIndexAnnotation = (IndexAnnotation)newIndexAnnotation.MergeWith(foreignKeyIndexAnnotation); |
62 | 63 | } |
| 64 | + |
| 65 | + edmProperty.AddAnnotation(IndexAnnotationName, newIndexAnnotation); |
63 | 66 | } |
64 | 67 | } |
65 | 68 |
|
|
0 commit comments