Skip to content

Commit 158a313

Browse files
committed
Coding Style.
1 parent 21add41 commit 158a313

File tree

6 files changed

+70
-27
lines changed

6 files changed

+70
-27
lines changed

SQLite.CodeFirst/Builder/ColumnStatementCollectionBuilder.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Collections.Generic;
22
using System.Data.Entity.Core.Metadata.Edm;
3+
using System.Globalization;
34
using System.Linq;
45
using SQLite.CodeFirst.Statement;
56
using SQLite.CodeFirst.Statement.ColumnConstraint;
@@ -40,7 +41,7 @@ private IEnumerable<ColumnStatement> CreateColumnStatements()
4041
}
4142
}
4243

43-
private void AddMaxLengthConstraintIfNecessary(EdmProperty property, ColumnStatement columnStatement)
44+
private static void AddMaxLengthConstraintIfNecessary(EdmProperty property, ColumnStatement columnStatement)
4445
{
4546
if (property.MaxLength.HasValue)
4647
{
@@ -53,7 +54,7 @@ private static void AdjustDatatypeForAutogenerationIfNecessary(EdmProperty prope
5354
if (property.StoreGeneratedPattern == StoreGeneratedPattern.Identity)
5455
{
5556
// Must be INTEGER else SQLite will not generate the Ids
56-
columnStatement.TypeName = columnStatement.TypeName.ToLower() == "int" ? "INTEGER" : columnStatement.TypeName;
57+
columnStatement.TypeName = columnStatement.TypeName.ToLower(CultureInfo.InvariantCulture) == "int" ? "INTEGER" : columnStatement.TypeName;
5758
}
5859
}
5960

SQLite.CodeFirst/Builder/CreateIndexStatementBuilder.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using System.Collections.ObjectModel;
34
using System.ComponentModel.DataAnnotations.Schema;
45
using System.Data.Entity.Core.Metadata.Edm;
56
using System.Data.Entity.Infrastructure.Annotations;
7+
using System.Globalization;
68
using System.Linq;
79
using SQLite.CodeFirst.Extensions;
810
using SQLite.CodeFirst.Statement;
@@ -57,7 +59,7 @@ public CreateIndexStatementCollection BuildStatement()
5759

5860
private string GetIndexName(IndexAttribute index, EdmProperty property)
5961
{
60-
return index.Name ?? string.Format("IX_{0}_{1}", entitySet.ElementType.GetTableName(), property.Name);
62+
return index.Name ?? String.Format(CultureInfo.InvariantCulture, "IX_{0}_{1}", entitySet.ElementType.GetTableName(), property.Name);
6163
}
6264
}
6365
}

SQLite.CodeFirst/Convention/SqliteForeignKeyIndexConvention.cs

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Data.Entity.Infrastructure;
77
using System.Data.Entity.Infrastructure.Annotations;
88
using System.Data.Entity.ModelConfiguration.Conventions;
9+
using System.Globalization;
910
using System.Linq;
1011

1112
namespace SQLite.CodeFirst.Convention
@@ -35,31 +36,33 @@ public virtual void Apply(AssociationType item, DbModel model)
3536
return;
3637
}
3738

38-
for (int i = 0; i < item.Constraint.ToProperties.Count; i++)
39+
foreach (var edmProperty in item.Constraint.ToProperties)
3940
{
40-
EdmProperty edmProperty = item.Constraint.ToProperties[i];
4141
var annotation = GetAnnotation(edmProperty.MetadataProperties, IndexAnnotationName);
42-
if (annotation != null)
42+
if (annotation == null)
4343
{
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+
}
4746

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);
5150

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);
6054

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);
6263
}
64+
65+
edmProperty.AddAnnotation(IndexAnnotationName, newIndexAnnotation);
6366
}
6467
}
6568

SQLite.CodeFirst/SqliteConnectionStringParser.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Globalization;
34

45
namespace SQLite.CodeFirst
56
{
@@ -20,8 +21,9 @@ public static IDictionary<string, string> ParseSqliteConnectionString(string con
2021
foreach (var keyValuePair in keyValuePairs)
2122
{
2223
string[] keyValue = keyValuePair.Split(KeyValueSeperator);
23-
if (keyValue.Length >= 2){
24-
keyValuePairDictionary.Add(keyValue[KeyPosition].ToLower(), keyValue[ValuePosition]);
24+
if (keyValue.Length >= 2)
25+
{
26+
keyValuePairDictionary.Add(keyValue[KeyPosition].ToLower(CultureInfo.InvariantCulture), keyValue[ValuePosition]);
2527
}
2628
}
2729

@@ -47,7 +49,7 @@ private static string ExpandDataDirectory(string path)
4749

4850
// find the replacement path
4951
object rootFolderObject = AppDomain.CurrentDomain.GetData("DataDirectory");
50-
string rootFolderPath = (rootFolderObject as string);
52+
string rootFolderPath = rootFolderObject as string;
5153
if (rootFolderObject != null && rootFolderPath == null)
5254
{
5355
throw new InvalidOperationException("The value stored in the AppDomains 'DataDirectory' variable has to be a string!");
@@ -56,7 +58,7 @@ private static string ExpandDataDirectory(string path)
5658
{
5759
rootFolderPath = AppDomain.CurrentDomain.BaseDirectory;
5860
}
59-
61+
6062
// We don't know if rootFolderpath ends with '\', and we don't know if the given name starts with onw
6163
int fileNamePosition = DataDirectoryToken.Length; // filename starts right after the '|datadirectory|' keyword
6264
bool rootFolderEndsWith = (0 < rootFolderPath.Length) && rootFolderPath[rootFolderPath.Length - 1] == '\\';

SQLite.CodeFirst/SqliteInitializerBase.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,29 @@
55

66
namespace SQLite.CodeFirst
77
{
8+
/// <summary>
9+
/// An basic implementation of the <see cref="IDatabaseInitializer{TContext}"/> interface.
10+
/// This class provides common logic which can be used when writing an Sqlite-Initializer.
11+
/// The logic provided is:
12+
/// 1. Remove/Add specific Conventions
13+
/// 2. Get the path to the database file
14+
/// 3. Create a new SQLite-Database from the model (Code First)
15+
/// 4. Seed data to the new created database
16+
/// The following implementations are provided: <see cref="T:SQLite.CodeFirst.SqliteCreateDatabaseIfNotExists`1"/>, <see cref="T:SQlite.CodeFirst.SqliteDropCreateDatabaseAlways`1"/>.
17+
/// </summary>
18+
/// <typeparam name="TContext">The type of the context.</typeparam>
819
public abstract class SqliteInitializerBase<TContext> : IDatabaseInitializer<TContext>
920
where TContext : DbContext
1021
{
1122
private readonly DbModelBuilder modelBuilder;
1223

1324
protected SqliteInitializerBase(DbModelBuilder modelBuilder)
1425
{
26+
if (modelBuilder == null)
27+
{
28+
throw new ArgumentNullException(nameof(modelBuilder));
29+
}
30+
1531
this.modelBuilder = modelBuilder;
1632

1733
// This convention will crash the SQLite Provider before "InitializeDatabase" gets called.
@@ -23,6 +39,13 @@ protected SqliteInitializerBase(DbModelBuilder modelBuilder)
2339
modelBuilder.Conventions.AddAfter<ForeignKeyIndexConvention>(new SqliteForeignKeyIndexConvention());
2440
}
2541

42+
/// <summary>
43+
/// Initialize the database for the given context.
44+
/// Generates the SQLite-DDL from the model and executs it against the database.
45+
/// After that the <see cref="Seed"/> method is executed.
46+
/// All actions are be executed in transactions.
47+
/// </summary>
48+
/// <param name="context">The context. </param>
2649
public virtual void InitializeDatabase(TContext context)
2750
{
2851
var model = modelBuilder.Build(context.Database.Connection);
@@ -58,8 +81,18 @@ public virtual void InitializeDatabase(TContext context)
5881
}
5982
}
6083

84+
/// <summary>
85+
/// Is executed right after the initialization <seealso cref="InitializeDatabase"/>.
86+
/// Use this method to seed data into the empty database.
87+
/// </summary>
88+
/// <param name="context">The context.</param>
6189
protected virtual void Seed(TContext context) { }
6290

91+
/// <summary>
92+
/// Gets the database path file path from a <see cref="TContext"/>.
93+
/// </summary>
94+
/// <param name="context">The context to get the database file path from.</param>
95+
/// <returns>The full path to the SQLite database file.</returns>
6396
protected string GetDatabasePathFromContext(TContext context)
6497
{
6598
return SqliteConnectionStringParser.GetDataSource(context.Database.Connection.ConnectionString);

SQLite.CodeFirst/Statement/ColumnConstraint/ColumnConstraintCollection.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ internal class ColumnConstraintCollection : Collection<IColumnConstraint>, IColu
99
{
1010
private const string ConstraintStatementSeperator = " ";
1111

12-
public ColumnConstraintCollection() { }
12+
public ColumnConstraintCollection()
13+
: this(new List<IColumnConstraint>())
14+
{ }
1315

1416
public ColumnConstraintCollection(IEnumerable<IColumnConstraint> columnConstraints)
1517
{

0 commit comments

Comments
 (0)