Skip to content

Commit 7afbca0

Browse files
committed
Corrected/added formatting and comments.
1 parent 53a49f8 commit 7afbca0

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

SQLite.CodeFirst/Convention/SqliteForeignKeyIndexConvention.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@
99

1010
namespace SQLite.CodeFirst.Convention
1111
{
12+
/// <summary>
13+
/// Renames the indicies, generated by the
14+
/// <see cref="ForeignKeyIndexConvention"/>-Convention, to match the scheme: "IX_{TableName}_{PropertyName}".
15+
/// This is necessary because in SQLite an index-name must be unique.
16+
/// Must be added right after the <see cref="ForeignKeyIndexConvention"/>-Convention.
17+
/// </summary>
1218
public class SqliteForeignKeyIndexConvention : IStoreModelConvention<AssociationType>
1319
{
1420
private const string IndexAnnotationName = "http://schemas.microsoft.com/ado/2013/11/edm/customannotation:Index";
@@ -34,13 +40,15 @@ public virtual void Apply(AssociationType item, DbModel model)
3440
var annotation = GetAnnotation(edmProperty.MetadataProperties, IndexAnnotationName);
3541
if (annotation != null)
3642
{
37-
// The original attribute is removed. The noneForeignKeyIndicies will be remained and readded without any modification
43+
// The original attribute is removed. The none-ForeignKeyIndicies will be remained and readded without any modification
3844
// and the foreignKeyIncidies will be readded with the correct name.
3945
edmProperty.RemoveAnnotation(IndexAnnotationName);
4046

47+
// The schema for the automatically generated index name is "IX_{TableName}_{PropertyName}"
4148
var noneForeignKeyIndicies = annotation.Indexes.Where(index => index.Name != "IX_" + edmProperty.Name);
4249
IndexAnnotation newIndexAnnotation = new IndexAnnotation(noneForeignKeyIndicies);
4350

51+
// The schema for a FK index, which is generated by the Entity Framework, is "IX_{PropertyName}"
4452
var foreignKeyIndicies = annotation.Indexes.Where(index => index.Name == "IX_" + edmProperty.Name);
4553
foreach (var foreignKeyIndex in foreignKeyIndicies)
4654
{
@@ -54,7 +62,6 @@ public virtual void Apply(AssociationType item, DbModel model)
5462
}
5563
}
5664

57-
5865
private static IndexAnnotation GetAnnotation(IEnumerable<MetadataProperty> metadataProperties, string name)
5966
{
6067
foreach (MetadataProperty metadataProperty in metadataProperties)

SQLite.CodeFirst/SqliteInitializerBase.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System;
22
using System.Data.Entity;
33
using System.Data.Entity.ModelConfiguration.Conventions;
4-
using SQLite.CodeFirst.Convention;
4+
using SQLite.CodeFirst.Convention;
55

66
namespace SQLite.CodeFirst
77
{
@@ -17,6 +17,9 @@ protected SqliteInitializerBase(DbModelBuilder modelBuilder)
1717
// This convention will crash the SQLite Provider before "InitializeDatabase" gets called.
1818
// See https://github.com/msallin/SQLiteCodeFirst/issues/7 for details.
1919
modelBuilder.Conventions.Remove<TimestampAttributeConvention>();
20+
21+
// Place the own ForeinKeyIndexConvention right after the original.
22+
// The own convention will rename the automatically created indicies by using the correct scheme.
2023
modelBuilder.Conventions.AddAfter<ForeignKeyIndexConvention>(new SqliteForeignKeyIndexConvention());
2124
}
2225

0 commit comments

Comments
 (0)