Skip to content

Commit 915eb03

Browse files
committed
Merge branch 'master' into Issue_21
Conflicts: SQLite.CodeFirst/Builder/CreateIndexStatementBuilder.cs
2 parents 89c174a + 8873148 commit 915eb03

12 files changed

+62
-55
lines changed

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
Creates a [SQLite Database](https://sqlite.org/) from Code, using [Entity Framework](https://msdn.microsoft.com/en-us/data/ef.aspx) CodeFirst.
77

8-
This Project ships several `IDbInitializer` which creates a new SQLite Database, based on your model/code.
8+
This Project ships several `IDbInitializer` classes. These create new SQLite Databases based on your model/code.
99
I started with the [code](https://gist.github.com/flaub/1968486e1b3f2b9fddaf) from [flaub](https://github.com/flaub).
1010

1111
Currently the following is supported:
@@ -18,23 +18,23 @@ Currently the following is supported:
1818
- Index (Decorate columns with the `Index` attribute. Indices are automatically created for foreign keys by default. To prevent this you can remove the convetion `ForeignKeyIndexConvention`)
1919

2020
I tried to write the code in a extensible way.
21-
The logic is divided into two main parts. Builder and Statement.
21+
The logic is divided into two main parts, Builder and Statement.
2222
The Builder knows how to translate the EdmModel into statements where a statement class creates the SQLite-DDL-Code.
2323
The structure of the statements is influenced by the [SQLite Language Specification](https://www.sqlite.org/lang.html).
2424
You will find an extensive usage of the composite pattern.
2525

2626
## Install
2727
Either get the assembly from the latest [GitHub Release Page](https://github.com/msallin/SQLiteCodeFirst/releases) or install the NuGet-Package [SQLite.CodeFirst](https://www.nuget.org/packages/SQLite.CodeFirst/) (`PM> Install-Package SQLite.CodeFirst`).
2828

29-
The project is builded for the target frameworks 4.0 and 4.5.
30-
That means you can use the SQLite CodeFirst in projects with the following target frameworks.
29+
The project is built to target.NET framework versions 4.0 and 4.5.
30+
You can use the SQLite CodeFirst in projects that target the following frameworks:
3131
- .NET 4.0 (use net40)
3232
- .NET 4.5 (use net45)
3333
- .NET 4.5.1 (use net45)
3434
- .NET 4.5.2 (use net45)
3535

3636
## How to use
37-
If you want to let the Entity Framework create the database, if it does not exist, just set `SqliteDropCreateDatabaseAlways<>` or `SqliteCreateDatabaseIfNotExists<>` as your `IDbInitializer<>`.
37+
When you want to let the Entity Framework create database if it does not exist, just set `SqliteDropCreateDatabaseAlways<>` or `SqliteCreateDatabaseIfNotExists<>` as your `IDbInitializer<>`.
3838
```csharp
3939
public class MyDbContext : DbContext
4040
{
@@ -50,9 +50,9 @@ public class MyDbContext : DbContext
5050
}
5151
```
5252

53-
In a more advanced scenario you may want to populate some core- or test-data after the database was created.
53+
In a more advanced scenario, you may want to populate some core- or test-data after the database was created.
5454
To do this, inherit from `SqliteDropCreateDatabaseAlways<>` or `SqliteCreateDatabaseIfNotExists<>` and override the `Seed(MyDbContext context)` function.
55-
This function will be called, in a own transaction, right after the database was created. This function is only executed if a new database was successfully created.
55+
This function will be called in a transaction once the database was created. This function is only executed if a new database was successfully created.
5656
```csharp
5757
public class MyDbContextContextInitializer : SqliteDropCreateDatabaseAlways<MyDbContext>
5858
{
@@ -67,5 +67,5 @@ public class MyDbContextContextInitializer : SqliteDropCreateDatabaseAlways<MyDb
6767
```
6868

6969
## Hints
70-
If you try to reinstall the NuGet-Packages (e.g. if you want to downgrade to .NET 4.0) the app.config will be overwritten and you may getting an exception when you try to run the console project.
70+
If you try to reinstall the NuGet-Packages (e.g. if you want to downgrade to .NET 4.0), the app.config will be overwritten and you may getting an exception when you try to run the console project.
7171
In this case please check the following issue: https://github.com/msallin/SQLiteCodeFirst/issues/13.

SQLite.CodeFirst.Console/Entity/Player.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ public class Player : IEntity
88
{
99
public int Id { get; set; }
1010

11-
[Index] // Automatically named 'IX_FirstName'
11+
[Index] // Automatically named 'IX_TeamPlayer_FirstName'
1212
[MaxLength(50)]
1313
public string FirstName { get; set; }
1414

15-
[Index("IX_LN")]
15+
[Index("IX_TeamPlayer_LN")] // Test for named index
1616
[MaxLength(50)]
1717
public string LastName { get; set; }
1818

SQLite.CodeFirst.Console/Entity/Stadion.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ public class Stadion
77
{
88
[Key]
99
[Column(Order = 1)]
10-
[Index("IX_Main", Order = 2)]
11-
public string Name { get; set; }
10+
[Index("IX_Stadion_Main", Order = 2)] // Test for combined, named index
11+
public string Name { get; set; }
1212

1313
[Key]
1414
[Column(Order = 2)]
15-
[Index("IX_Main", Order = 1)]
15+
[Index("IX_Stadion_Main", Order = 1)] // Test for combined, named index
1616
public string Street { get; set; }
1717

1818
[Key]

SQLite.CodeFirst.Console/FootballDbContext.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ protected override void OnModelCreating(DbModelBuilder modelBuilder)
2222

2323
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
2424

25-
var initializer = new FootballDbInitializer(Database.Connection.ConnectionString, modelBuilder);
25+
var initializer = new FootballDbInitializer(modelBuilder);
2626
Database.SetInitializer(initializer);
2727
}
2828

@@ -47,8 +47,8 @@ private static void ConfigurePlayerEntity(DbModelBuilder modelBuilder)
4747

4848
public class FootballDbInitializer : SqliteDropCreateDatabaseAlways<FootballDbContext>
4949
{
50-
public FootballDbInitializer(string connectionString, DbModelBuilder modelBuilder)
51-
: base(connectionString, modelBuilder) { }
50+
public FootballDbInitializer(DbModelBuilder modelBuilder)
51+
: base(modelBuilder) { }
5252

5353
protected override void Seed(FootballDbContext context)
5454
{

SQLite.CodeFirst/Builder/CreateDatabaseStatementBuilder.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,21 @@ public CreateDatabaseStatement BuildStatement()
2525

2626
private IEnumerable<CreateTableStatement> GetCreateTableStatements()
2727
{
28-
foreach (var entityType in edmModel.EntityTypes)
28+
foreach (var entitySet in edmModel.Container.EntitySets)
2929
{
3030
ICollection<AssociationType> associationTypes =
31-
edmModel.AssociationTypes.Where(a => a.Constraint.ToRole.Name == entityType.Name).ToList();
31+
edmModel.AssociationTypes.Where(a => a.Constraint.ToRole.Name == entitySet.Name).ToList();
3232

33-
var tableStatementBuilder = new CreateTableStatementBuilder(entityType, associationTypes);
33+
var tableStatementBuilder = new CreateTableStatementBuilder(entitySet, associationTypes);
3434
yield return tableStatementBuilder.BuildStatement();
3535
}
3636
}
3737

3838
private IEnumerable<CreateIndexStatementCollection> GetCreateIndexStatements()
3939
{
40-
foreach (var entityType in edmModel.EntityTypes)
40+
foreach (var entitySet in edmModel.Container.EntitySets)
4141
{
42-
var indexStatementBuilder = new CreateIndexStatementBuilder(entityType);
42+
var indexStatementBuilder = new CreateIndexStatementBuilder(entitySet);
4343
yield return indexStatementBuilder.BuildStatement();
4444
}
4545
}

SQLite.CodeFirst/Builder/CreateIndexStatementBuilder.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,18 @@ namespace SQLite.CodeFirst.Builder
1111
{
1212
internal class CreateIndexStatementBuilder : IStatementBuilder<CreateIndexStatementCollection>
1313
{
14-
private readonly EntityType entityType;
14+
private readonly EntitySet entitySet;
1515

16-
public CreateIndexStatementBuilder(EntityType entityType)
16+
public CreateIndexStatementBuilder(EntitySet entitySet)
1717
{
18-
this.entityType = entityType;
18+
this.entitySet = entitySet;
1919
}
2020

2121
public CreateIndexStatementCollection BuildStatement()
2222
{
2323
IDictionary<string, CreateIndexStatement> createIndexStatments = new Dictionary<string, CreateIndexStatement>();
2424

25-
foreach (var edmProperty in entityType.Properties)
25+
foreach (var edmProperty in entitySet.ElementType.Properties)
2626
{
2727
var indexAnnotations = edmProperty.MetadataProperties
2828
.Select(x => x.Value)
@@ -38,7 +38,7 @@ public CreateIndexStatementCollection BuildStatement()
3838
{
3939
IsUnique = index.IsUnique,
4040
Name = indexName,
41-
Table = entityType.GetTableName(),
41+
Table = entitySet.Table,
4242
Columns = new Collection<CreateIndexStatement.IndexColumn>()
4343
};
4444
createIndexStatments.Add(indexName, createIndexStatement);
@@ -55,9 +55,9 @@ public CreateIndexStatementCollection BuildStatement()
5555
return new CreateIndexStatementCollection(createIndexStatments.Values);
5656
}
5757

58-
private static string GetIndexName(EntityType entityType, EdmProperty property, IndexAttribute index)
58+
private string GetIndexName(IndexAttribute index, EdmProperty property)
5959
{
60-
return index.Name ?? string.Format("IX_{0}_{1}", entityType.GetTableName(), property.Name);
60+
return index.Name ?? string.Format("IX_{0}_{1}", entitySet.ElementType.GetTableName(), property.Name);
6161
}
6262
}
6363
}

SQLite.CodeFirst/Builder/CreateTableStatementBuilder.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,19 @@ namespace SQLite.CodeFirst.Builder
77
{
88
internal class CreateTableStatementBuilder : IStatementBuilder<CreateTableStatement>
99
{
10-
private readonly EntityType entityType;
10+
private readonly EntitySet entitySet;
1111
private readonly IEnumerable<AssociationType> associationTypes;
1212

13-
public CreateTableStatementBuilder(EntityType entityType, IEnumerable<AssociationType> associationTypes)
13+
public CreateTableStatementBuilder(EntitySet entitySet, IEnumerable<AssociationType> associationTypes)
1414
{
15-
this.entityType = entityType;
15+
this.entitySet = entitySet;
1616
this.associationTypes = associationTypes;
1717
}
1818

1919
public CreateTableStatement BuildStatement()
2020
{
21-
var simpleColumnCollection = new ColumnStatementCollectionBuilder(entityType.Properties).BuildStatement();
22-
var primaryKeyStatement = new PrimaryKeyStatementBuilder(entityType.KeyMembers).BuildStatement();
21+
var simpleColumnCollection = new ColumnStatementCollectionBuilder(entitySet.ElementType.Properties).BuildStatement();
22+
var primaryKeyStatement = new PrimaryKeyStatementBuilder(entitySet.ElementType.KeyMembers).BuildStatement();
2323
var foreignKeyCollection = new ForeignKeyStatementBuilder(associationTypes).BuildStatement();
2424

2525
var columnStatements = new List<IStatement>();
@@ -29,7 +29,7 @@ public CreateTableStatement BuildStatement()
2929

3030
return new CreateTableStatement
3131
{
32-
TableName = entityType.GetTableName(),
32+
TableName = entitySet.Table,
3333
ColumnStatementCollection = new ColumnStatementCollection(columnStatements)
3434
};
3535
}

SQLite.CodeFirst/SqliteConnectionStringParser.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ public static IDictionary<string, string> ParseSqliteConnectionString(string con
2020
foreach (var keyValuePair in keyValuePairs)
2121
{
2222
string[] keyValue = keyValuePair.Split(KeyValueSeperator);
23-
keyValuePairDictionary.Add(keyValue[KeyPosition].ToLower(), keyValue[ValuePosition]);
23+
if (keyValue.Length >= 2){
24+
keyValuePairDictionary.Add(keyValue[KeyPosition].ToLower(), keyValue[ValuePosition]);
25+
}
2426
}
2527

2628
return keyValuePairDictionary;
@@ -29,6 +31,8 @@ public static IDictionary<string, string> ParseSqliteConnectionString(string con
2931
public static string GetDataSource(string connectionString)
3032
{
3133
var path = ExpandDataDirectory(ParseSqliteConnectionString(connectionString)["data source"]);
34+
// remove quotation mark if exists
35+
path = path.Trim('"');
3236
return path;
3337
}
3438

SQLite.CodeFirst/SqliteCreateDatabaseIfNotExists.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ namespace SQLite.CodeFirst
66
public class SqliteCreateDatabaseIfNotExists<TContext> : SqliteInitializerBase<TContext>
77
where TContext : DbContext
88
{
9-
public SqliteCreateDatabaseIfNotExists(string connectionString, DbModelBuilder modelBuilder)
10-
: base(connectionString, modelBuilder) { }
9+
public SqliteCreateDatabaseIfNotExists(DbModelBuilder modelBuilder)
10+
: base(modelBuilder) { }
1111

1212
public override void InitializeDatabase(TContext context)
1313
{
14-
bool dbExists = File.Exists(DatabaseFilePath);
14+
string databseFilePath = GetDatabasePathFromContext(context);
15+
16+
bool dbExists = File.Exists(databseFilePath);
1517
if (dbExists)
1618
{
1719
return;

SQLite.CodeFirst/SqliteDropCreateDatabaseAlways.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,17 @@ namespace SQLite.CodeFirst
66
public class SqliteDropCreateDatabaseAlways<TContext> : SqliteInitializerBase<TContext>
77
where TContext : DbContext
88
{
9-
public SqliteDropCreateDatabaseAlways(string connectionString, DbModelBuilder modelBuilder)
10-
: base(connectionString, modelBuilder) { }
9+
public SqliteDropCreateDatabaseAlways(DbModelBuilder modelBuilder)
10+
: base(modelBuilder) { }
1111

1212
public override void InitializeDatabase(TContext context)
1313
{
14-
bool dbExists = File.Exists(DatabaseFilePath);
14+
string databseFilePath = GetDatabasePathFromContext(context);
15+
16+
bool dbExists = File.Exists(databseFilePath);
1517
if (dbExists)
1618
{
17-
File.Delete(DatabaseFilePath);
19+
File.Delete(databseFilePath);
1820
}
1921

2022
base.InitializeDatabase(context);

0 commit comments

Comments
 (0)