Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 41 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,18 @@ You can use the SQLite CodeFirst in projects that target the following framework
- .NET 4.6.1 (use net45)

## How to use
The functionality is exposed by using implementations of the `IDbInitializer<>` interface.
Depending on your need, you can choose from the following initializers:
- SqliteCreateDatabaseIfNotExists
- SqliteDropCreateDatabaseAlways
- SqliteDropCreateDatabaseWhenModelChanges

If you want to have more control, you can use the `SqliteDatabaseCreator` (implements `IDatabaseCreator`) which lets you control the creation of the SQLite database.
Or for even more control, use the `SqliteSqlGenerator` (implements `ISqlGenerator`), which lets you generate the SQL code based on your `EdmModel`.

When you want to let the Entity Framework create database if it does not exist, just set `SqliteDropCreateDatabaseAlways<>` or `SqliteCreateDatabaseIfNotExists<>` as your `IDbInitializer<>`.

### Initializer Sample
```csharp
public class MyDbContext : DbContext
{
Expand All @@ -56,9 +67,12 @@ public class MyDbContext : DbContext
}
}
```
Notice that the `SqliteDropCreateDatabaseWhenModelChanges<>` initializer will create a additional table in your database.
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
`IHistory` interface and pass the type of your entity as parameter in the to the constructor from the initializer.

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

### SqliteDatabaseCreator Sample
```csharp
public class MyContext : DbContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
var model = modelBuilder.Build(Database.Connection);
IDatabaseCreator sqliteDatabaseCreator = new SqliteDatabaseCreator();
sqliteDatabaseCreator.Create(Database, model);
}
}
```

### SqliteSqlGenerator Sample
```csharp
public class MyContext : DbContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
var model = modelBuilder.Build(Database.Connection);
ISqlGenerator sqlGenerator = new SqliteSqlGenerator();
string sql = sqlGenerator.Generate(model.StoreModel);
}
}
```

## Hints
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.
In this case please check the following issue: https://github.com/msallin/SQLiteCodeFirst/issues/13.
6 changes: 4 additions & 2 deletions SQLite.CodeFirst.Console/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
</startup>
<system.data>
<DbProviderFactories>
<remove invariant="System.Data.SQLite"/>
<remove invariant="System.Data.SQLite.EF6" />
<add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".Net Framework Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" />
<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" />
<remove invariant="System.Data.SQLite" />
<add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".NET Framework Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" />
</DbProviderFactories>
</system.data>
<entityFramework>
Expand All @@ -25,6 +26,7 @@
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
<provider invariantName="System.Data.SQLite" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
<provider invariantName="System.Data.SQLite.EF6" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
</providers>
</entityFramework>
</configuration>
12 changes: 12 additions & 0 deletions SQLite.CodeFirst.Console/Entity/CustomHistory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;

namespace SQLite.CodeFirst.Console.Entity
{
public class CustomHistory : IHistory
{
public int Id { get; set; }
public string Hash { get; set; }
public string Context { get; set; }
public DateTime CreateDate { get; set; }
}
}
53 changes: 1 addition & 52 deletions SQLite.CodeFirst.Console/FootballDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Data.Entity;
using SQLite.CodeFirst.Console.Entity;

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

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

ConfigureTeamEntity(modelBuilder);
ConfigureStadionEntity(modelBuilder);
ConfigureCoachEntity(modelBuilder);
Expand Down Expand Up @@ -61,51 +57,4 @@ private static void ConfigurePlayerEntity(DbModelBuilder modelBuilder)
.WillCascadeOnDelete(true);
}
}

public class FootballDbInitializer : SqliteDropCreateDatabaseAlways<FootballDbContext>
{
public FootballDbInitializer(DbModelBuilder modelBuilder)
: base(modelBuilder)
{ }

protected override void Seed(FootballDbContext context)
{
context.Set<Team>().Add(new Team
{
Name = "YB",
Coach = new Coach
{
City = "Zürich",
FirstName = "Masssaman",
LastName = "Nachn",
Street = "Testingstreet 844"
},
Players = new List<Player>
{
new Player
{
City = "Bern",
FirstName = "Marco",
LastName = "Bürki",
Street = "Wunderstrasse 43",
Number = 12
},
new Player
{
City = "Berlin",
FirstName = "Alain",
LastName = "Rochat",
Street = "Wonderstreet 13",
Number = 14
}
},
Stadion = new Stadion
{
Name = "Stade de Suisse",
City = "Bern",
Street = "Papiermühlestrasse 71"
}
});
}
}
}
53 changes: 53 additions & 0 deletions SQLite.CodeFirst.Console/FootballDbInitializer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System.Collections.Generic;
using System.Data.Entity;
using SQLite.CodeFirst.Console.Entity;

namespace SQLite.CodeFirst.Console
{
public class FootballDbInitializer : SqliteDropCreateDatabaseWhenModelChanges<FootballDbContext>
{
public FootballDbInitializer(DbModelBuilder modelBuilder)
: base(modelBuilder, typeof(CustomHistory))
{ }

protected override void Seed(FootballDbContext context)
{
context.Set<Team>().Add(new Team
{
Name = "YB",
Coach = new Coach
{
City = "Z�rich",
FirstName = "Masssaman",
LastName = "Nachn",
Street = "Testingstreet 844"
},
Players = new List<Player>
{
new Player
{
City = "Bern",
FirstName = "Marco",
LastName = "B�rki",
Street = "Wunderstrasse 43",
Number = 12
},
new Player
{
City = "Berlin",
FirstName = "Alain",
LastName = "Rochat",
Street = "Wonderstreet 13",
Number = 14
}
},
Stadion = new Stadion
{
Name = "Stade de Suisse",
City = "Bern",
Street = "Papierm�hlestrasse 71"
}
});
}
}
}
20 changes: 12 additions & 8 deletions SQLite.CodeFirst.Console/SQLite.CodeFirst.Console.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,17 @@
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System.Configuration" />
<Reference Include="System.Core" />
<Reference Include="System.Data.SQLite, Version=1.0.97.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139, processorArchitecture=MSIL">
<HintPath>..\packages\System.Data.SQLite.Core.1.0.97.0\lib\net451\System.Data.SQLite.dll</HintPath>
<Reference Include="System.Data.SQLite, Version=1.0.99.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139, processorArchitecture=MSIL">
<HintPath>..\packages\System.Data.SQLite.Core.1.0.99.0\lib\net451\System.Data.SQLite.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Data.SQLite.EF6">
<HintPath>..\packages\System.Data.SQLite.EF6.1.0.97.0\lib\net451\System.Data.SQLite.EF6.dll</HintPath>
<Reference Include="System.Data.SQLite.EF6, Version=1.0.99.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139, processorArchitecture=MSIL">
<HintPath>..\packages\System.Data.SQLite.EF6.1.0.99.0\lib\net451\System.Data.SQLite.EF6.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Data.SQLite.Linq">
<HintPath>..\packages\System.Data.SQLite.Linq.1.0.97.0\lib\net451\System.Data.SQLite.Linq.dll</HintPath>
<Reference Include="System.Data.SQLite.Linq, Version=1.0.99.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139, processorArchitecture=MSIL">
<HintPath>..\packages\System.Data.SQLite.Linq.1.0.99.0\lib\net451\System.Data.SQLite.Linq.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
Expand All @@ -77,12 +79,14 @@
<Link>Properties\AssemblyVersionInfo.cs</Link>
</Compile>
<Compile Include="Entity\Coach.cs" />
<Compile Include="Entity\CustomHistory.cs" />
<Compile Include="Entity\IEntity.cs" />
<Compile Include="Entity\Person.cs" />
<Compile Include="Entity\Player.cs" />
<Compile Include="Entity\Stadion.cs" />
<Compile Include="Entity\Team.cs" />
<Compile Include="FootballDbContext.cs" />
<Compile Include="FootballDbInitializer.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
Expand All @@ -102,10 +106,10 @@
<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>
</PropertyGroup>
<Error Condition="!Exists('$(SolutionDir)\.nuget\NuGet.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(SolutionDir)\.nuget\NuGet.targets'))" />
<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'))" />
<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'))" />
</Target>
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
<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')" />
<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')" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
Expand Down
8 changes: 4 additions & 4 deletions SQLite.CodeFirst.Console/packages.config
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="EntityFramework" version="6.1.3" targetFramework="net452" userInstalled="true" />
<package id="System.Data.SQLite" version="1.0.97.0" targetFramework="net452" userInstalled="true" />
<package id="System.Data.SQLite.Core" version="1.0.97.0" targetFramework="net452" userInstalled="true" />
<package id="System.Data.SQLite.EF6" version="1.0.97.0" targetFramework="net452" userInstalled="true" />
<package id="System.Data.SQLite.Linq" version="1.0.97.0" targetFramework="net452" userInstalled="true" />
<package id="System.Data.SQLite" version="1.0.99.0" targetFramework="net452" userInstalled="true" />
<package id="System.Data.SQLite.Core" version="1.0.99.0" targetFramework="net452" userInstalled="true" />
<package id="System.Data.SQLite.EF6" version="1.0.99.0" targetFramework="net452" userInstalled="true" />
<package id="System.Data.SQLite.Linq" version="1.0.99.0" targetFramework="net452" userInstalled="true" />
</packages>
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public override void InitializeDatabase(TContext context)
{
string databseFilePath = GetDatabasePathFromContext(context);

bool dbExists = File.Exists(databseFilePath);
if (dbExists)
bool exists = File.Exists(databseFilePath);
if (exists)
{
File.Delete(databseFilePath);
}
Expand Down
Loading