Skip to content

Commit ac87d30

Browse files
committed
Merge pull request #55 from msallin/Issue-16
Issue 16
2 parents 280a3c5 + 54050e5 commit ac87d30

File tree

53 files changed

+512
-146
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+512
-146
lines changed

README.md

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,18 @@ You can use the SQLite CodeFirst in projects that target the following framework
4242
- .NET 4.6.1 (use net45)
4343

4444
## How to use
45+
The functionality is exposed by using implementations of the `IDbInitializer<>` interface.
46+
Depending on your need, you can choose from the following initializers:
47+
- SqliteCreateDatabaseIfNotExists
48+
- SqliteDropCreateDatabaseAlways
49+
- SqliteDropCreateDatabaseWhenModelChanges
50+
51+
If you want to have more control, you can use the `SqliteDatabaseCreator` (implements `IDatabaseCreator`) which lets you control the creation of the SQLite database.
52+
Or for even more control, use the `SqliteSqlGenerator` (implements `ISqlGenerator`), which lets you generate the SQL code based on your `EdmModel`.
53+
4554
When you want to let the Entity Framework create database if it does not exist, just set `SqliteDropCreateDatabaseAlways<>` or `SqliteCreateDatabaseIfNotExists<>` as your `IDbInitializer<>`.
55+
56+
### Initializer Sample
4657
```csharp
4758
public class MyDbContext : DbContext
4859
{
@@ -56,9 +67,12 @@ public class MyDbContext : DbContext
5667
}
5768
}
5869
```
70+
Notice that the `SqliteDropCreateDatabaseWhenModelChanges<>` initializer will create a additional table in your database.
71+
This table is used to store some information to detect model changes. If you want to use a own entity/table you can implement the
72+
`IHistory` interface and pass the type of your entity as parameter in the to the constructor from the initializer.
5973

6074
In a more advanced scenario, you may want to populate some core- or test-data after the database was created.
61-
To do this, inherit from `SqliteDropCreateDatabaseAlways<>` or `SqliteCreateDatabaseIfNotExists<>` and override the `Seed(MyDbContext context)` function.
75+
To do this, inherit from `SqliteDropCreateDatabaseAlways<>`, `SqliteCreateDatabaseIfNotExists<>` or `SqliteDropCreateDatabaseWhenModelChanges<>` and override the `Seed(MyDbContext context)` function.
6276
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.
6377
```csharp
6478
public class MyDbContextInitializer : SqliteDropCreateDatabaseAlways<MyDbContext>
@@ -73,6 +87,32 @@ public class MyDbContextInitializer : SqliteDropCreateDatabaseAlways<MyDbContext
7387
}
7488
```
7589

90+
### SqliteDatabaseCreator Sample
91+
```csharp
92+
public class MyContext : DbContext
93+
{
94+
protected override void OnModelCreating(DbModelBuilder modelBuilder)
95+
{
96+
var model = modelBuilder.Build(Database.Connection);
97+
IDatabaseCreator sqliteDatabaseCreator = new SqliteDatabaseCreator();
98+
sqliteDatabaseCreator.Create(Database, model);
99+
}
100+
}
101+
```
102+
103+
### SqliteSqlGenerator Sample
104+
```csharp
105+
public class MyContext : DbContext
106+
{
107+
protected override void OnModelCreating(DbModelBuilder modelBuilder)
108+
{
109+
var model = modelBuilder.Build(Database.Connection);
110+
ISqlGenerator sqlGenerator = new SqliteSqlGenerator();
111+
string sql = sqlGenerator.Generate(model.StoreModel);
112+
}
113+
}
114+
```
115+
76116
## Hints
77117
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.
78118
In this case please check the following issue: https://github.com/msallin/SQLiteCodeFirst/issues/13.

SQLite.CodeFirst.Console/App.config

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@
1111
</startup>
1212
<system.data>
1313
<DbProviderFactories>
14-
<remove invariant="System.Data.SQLite"/>
1514
<remove invariant="System.Data.SQLite.EF6" />
16-
<add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".Net Framework Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" />
15+
<add name="SQLite Data Provider (Entity Framework 6)" invariant="System.Data.SQLite.EF6" description=".NET Framework Data Provider for SQLite (Entity Framework 6)" type="System.Data.SQLite.EF6.SQLiteProviderFactory, System.Data.SQLite.EF6" />
16+
<remove invariant="System.Data.SQLite" />
17+
<add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".NET Framework Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" />
1718
</DbProviderFactories>
1819
</system.data>
1920
<entityFramework>
@@ -25,6 +26,7 @@
2526
<providers>
2627
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
2728
<provider invariantName="System.Data.SQLite" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
29+
<provider invariantName="System.Data.SQLite.EF6" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
2830
</providers>
2931
</entityFramework>
3032
</configuration>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
3+
namespace SQLite.CodeFirst.Console.Entity
4+
{
5+
public class CustomHistory : IHistory
6+
{
7+
public int Id { get; set; }
8+
public string Hash { get; set; }
9+
public string Context { get; set; }
10+
public DateTime CreateDate { get; set; }
11+
}
12+
}
Lines changed: 1 addition & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using System.Collections.Generic;
2-
using System.Data.Entity;
3-
using System.Data.Entity.ModelConfiguration.Conventions;
1+
using System.Data.Entity;
42
using SQLite.CodeFirst.Console.Entity;
53

64
namespace SQLite.CodeFirst.Console
@@ -16,8 +14,6 @@ public FootballDbContext()
1614

1715
protected override void OnModelCreating(DbModelBuilder modelBuilder)
1816
{
19-
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
20-
2117
ConfigureTeamEntity(modelBuilder);
2218
ConfigureStadionEntity(modelBuilder);
2319
ConfigureCoachEntity(modelBuilder);
@@ -61,51 +57,4 @@ private static void ConfigurePlayerEntity(DbModelBuilder modelBuilder)
6157
.WillCascadeOnDelete(true);
6258
}
6359
}
64-
65-
public class FootballDbInitializer : SqliteDropCreateDatabaseAlways<FootballDbContext>
66-
{
67-
public FootballDbInitializer(DbModelBuilder modelBuilder)
68-
: base(modelBuilder)
69-
{ }
70-
71-
protected override void Seed(FootballDbContext context)
72-
{
73-
context.Set<Team>().Add(new Team
74-
{
75-
Name = "YB",
76-
Coach = new Coach
77-
{
78-
City = "Zürich",
79-
FirstName = "Masssaman",
80-
LastName = "Nachn",
81-
Street = "Testingstreet 844"
82-
},
83-
Players = new List<Player>
84-
{
85-
new Player
86-
{
87-
City = "Bern",
88-
FirstName = "Marco",
89-
LastName = "Bürki",
90-
Street = "Wunderstrasse 43",
91-
Number = 12
92-
},
93-
new Player
94-
{
95-
City = "Berlin",
96-
FirstName = "Alain",
97-
LastName = "Rochat",
98-
Street = "Wonderstreet 13",
99-
Number = 14
100-
}
101-
},
102-
Stadion = new Stadion
103-
{
104-
Name = "Stade de Suisse",
105-
City = "Bern",
106-
Street = "Papiermühlestrasse 71"
107-
}
108-
});
109-
}
110-
}
11160
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using System.Collections.Generic;
2+
using System.Data.Entity;
3+
using SQLite.CodeFirst.Console.Entity;
4+
5+
namespace SQLite.CodeFirst.Console
6+
{
7+
public class FootballDbInitializer : SqliteDropCreateDatabaseWhenModelChanges<FootballDbContext>
8+
{
9+
public FootballDbInitializer(DbModelBuilder modelBuilder)
10+
: base(modelBuilder, typeof(CustomHistory))
11+
{ }
12+
13+
protected override void Seed(FootballDbContext context)
14+
{
15+
context.Set<Team>().Add(new Team
16+
{
17+
Name = "YB",
18+
Coach = new Coach
19+
{
20+
City = "Zürich",
21+
FirstName = "Masssaman",
22+
LastName = "Nachn",
23+
Street = "Testingstreet 844"
24+
},
25+
Players = new List<Player>
26+
{
27+
new Player
28+
{
29+
City = "Bern",
30+
FirstName = "Marco",
31+
LastName = "Bürki",
32+
Street = "Wunderstrasse 43",
33+
Number = 12
34+
},
35+
new Player
36+
{
37+
City = "Berlin",
38+
FirstName = "Alain",
39+
LastName = "Rochat",
40+
Street = "Wonderstreet 13",
41+
Number = 14
42+
}
43+
},
44+
Stadion = new Stadion
45+
{
46+
Name = "Stade de Suisse",
47+
City = "Bern",
48+
Street = "Papiermühlestrasse 71"
49+
}
50+
});
51+
}
52+
}
53+
}

SQLite.CodeFirst.Console/SQLite.CodeFirst.Console.csproj

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,17 @@
5353
<Reference Include="System.ComponentModel.DataAnnotations" />
5454
<Reference Include="System.Configuration" />
5555
<Reference Include="System.Core" />
56-
<Reference Include="System.Data.SQLite, Version=1.0.97.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139, processorArchitecture=MSIL">
57-
<HintPath>..\packages\System.Data.SQLite.Core.1.0.97.0\lib\net451\System.Data.SQLite.dll</HintPath>
56+
<Reference Include="System.Data.SQLite, Version=1.0.99.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139, processorArchitecture=MSIL">
57+
<HintPath>..\packages\System.Data.SQLite.Core.1.0.99.0\lib\net451\System.Data.SQLite.dll</HintPath>
5858
<Private>True</Private>
5959
</Reference>
60-
<Reference Include="System.Data.SQLite.EF6">
61-
<HintPath>..\packages\System.Data.SQLite.EF6.1.0.97.0\lib\net451\System.Data.SQLite.EF6.dll</HintPath>
60+
<Reference Include="System.Data.SQLite.EF6, Version=1.0.99.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139, processorArchitecture=MSIL">
61+
<HintPath>..\packages\System.Data.SQLite.EF6.1.0.99.0\lib\net451\System.Data.SQLite.EF6.dll</HintPath>
62+
<Private>True</Private>
6263
</Reference>
63-
<Reference Include="System.Data.SQLite.Linq">
64-
<HintPath>..\packages\System.Data.SQLite.Linq.1.0.97.0\lib\net451\System.Data.SQLite.Linq.dll</HintPath>
64+
<Reference Include="System.Data.SQLite.Linq, Version=1.0.99.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139, processorArchitecture=MSIL">
65+
<HintPath>..\packages\System.Data.SQLite.Linq.1.0.99.0\lib\net451\System.Data.SQLite.Linq.dll</HintPath>
66+
<Private>True</Private>
6567
</Reference>
6668
<Reference Include="System.Xml.Linq" />
6769
<Reference Include="System.Data.DataSetExtensions" />
@@ -77,12 +79,14 @@
7779
<Link>Properties\AssemblyVersionInfo.cs</Link>
7880
</Compile>
7981
<Compile Include="Entity\Coach.cs" />
82+
<Compile Include="Entity\CustomHistory.cs" />
8083
<Compile Include="Entity\IEntity.cs" />
8184
<Compile Include="Entity\Person.cs" />
8285
<Compile Include="Entity\Player.cs" />
8386
<Compile Include="Entity\Stadion.cs" />
8487
<Compile Include="Entity\Team.cs" />
8588
<Compile Include="FootballDbContext.cs" />
89+
<Compile Include="FootballDbInitializer.cs" />
8690
<Compile Include="Program.cs" />
8791
<Compile Include="Properties\AssemblyInfo.cs" />
8892
</ItemGroup>
@@ -102,10 +106,10 @@
102106
<ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
103107
</PropertyGroup>
104108
<Error Condition="!Exists('$(SolutionDir)\.nuget\NuGet.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(SolutionDir)\.nuget\NuGet.targets'))" />
105-
<Error Condition="!Exists('..\packages\System.Data.SQLite.Core.1.0.97.0\build\net451\System.Data.SQLite.Core.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\System.Data.SQLite.Core.1.0.97.0\build\net451\System.Data.SQLite.Core.targets'))" />
109+
<Error Condition="!Exists('..\packages\System.Data.SQLite.Core.1.0.99.0\build\net451\System.Data.SQLite.Core.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\System.Data.SQLite.Core.1.0.99.0\build\net451\System.Data.SQLite.Core.targets'))" />
106110
</Target>
107111
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
108-
<Import Project="..\packages\System.Data.SQLite.Core.1.0.97.0\build\net451\System.Data.SQLite.Core.targets" Condition="Exists('..\packages\System.Data.SQLite.Core.1.0.97.0\build\net451\System.Data.SQLite.Core.targets')" />
112+
<Import Project="..\packages\System.Data.SQLite.Core.1.0.99.0\build\net451\System.Data.SQLite.Core.targets" Condition="Exists('..\packages\System.Data.SQLite.Core.1.0.99.0\build\net451\System.Data.SQLite.Core.targets')" />
109113
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
110114
Other similar extension points exist, see Microsoft.Common.targets.
111115
<Target Name="BeforeBuild">
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
33
<package id="EntityFramework" version="6.1.3" targetFramework="net452" userInstalled="true" />
4-
<package id="System.Data.SQLite" version="1.0.97.0" targetFramework="net452" userInstalled="true" />
5-
<package id="System.Data.SQLite.Core" version="1.0.97.0" targetFramework="net452" userInstalled="true" />
6-
<package id="System.Data.SQLite.EF6" version="1.0.97.0" targetFramework="net452" userInstalled="true" />
7-
<package id="System.Data.SQLite.Linq" version="1.0.97.0" targetFramework="net452" userInstalled="true" />
4+
<package id="System.Data.SQLite" version="1.0.99.0" targetFramework="net452" userInstalled="true" />
5+
<package id="System.Data.SQLite.Core" version="1.0.99.0" targetFramework="net452" userInstalled="true" />
6+
<package id="System.Data.SQLite.EF6" version="1.0.99.0" targetFramework="net452" userInstalled="true" />
7+
<package id="System.Data.SQLite.Linq" version="1.0.99.0" targetFramework="net452" userInstalled="true" />
88
</packages>

SQLite.CodeFirst/SqliteCreateDatabaseIfNotExists.cs renamed to SQLite.CodeFirst/DbInitializers/SqliteCreateDatabaseIfNotExists.cs

File renamed without changes.

SQLite.CodeFirst/SqliteDropCreateDatabaseAlways.cs renamed to SQLite.CodeFirst/DbInitializers/SqliteDropCreateDatabaseAlways.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ public override void InitializeDatabase(TContext context)
3030
{
3131
string databseFilePath = GetDatabasePathFromContext(context);
3232

33-
bool dbExists = File.Exists(databseFilePath);
34-
if (dbExists)
33+
bool exists = File.Exists(databseFilePath);
34+
if (exists)
3535
{
3636
File.Delete(databseFilePath);
3737
}

0 commit comments

Comments
 (0)