Skip to content

Commit 7358426

Browse files
committed
Merge remote-tracking branch 'refs/remotes/msallin/master'
Conflicts: README.md
2 parents c2f5cca + 48921db commit 7358426

66 files changed

Lines changed: 1414 additions & 198 deletions

File tree

Some content is hidden

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

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,6 @@ ClientBin/
155155
*~
156156
*.dbmdl
157157
*.dbproj.schemaview
158-
*.pfx
159158
*.publishsettings
160159
node_modules/
161160

BuildAllConfigurations.proj

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Rebuild" ToolsVersion="4.0">
3+
<ItemGroup>
4+
<ConfigurationList Include="Debug-40" />
5+
<ConfigurationList Include="Debug-45" />
6+
<ConfigurationList Include="Release-40" />
7+
<ConfigurationList Include="Release-45" />
8+
<PlatformList Include="Any CPU" />
9+
<TargetList Include="Build" />
10+
<ProjectList Include="$(MSBuildProjectDirectory)\SQLite.CodeFirst.sln" />
11+
</ItemGroup>
12+
13+
<Target Name="Rebuild" Outputs="%(PlatformList.Identity)">
14+
<PropertyGroup>
15+
<CurrentPlatform>
16+
%(PlatformList.Identity)
17+
</CurrentPlatform>
18+
</PropertyGroup>
19+
<MSBuild Projects="@(ProjectList)"
20+
Properties="Configuration=%(ConfigurationList.Identity);Platform=$(CurrentPlatform);"
21+
Targets="Rebuild"
22+
SkipNonexistentProjects="true" />
23+
</Target>
24+
</Project>

Lib/Moq.dll

649 KB
Binary file not shown.

README.md

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,36 @@
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:
1212
- Tables from classes (supported annotations: `Table`)
13-
- Columns from properties (supported annotations: `Column`, `Key`, `MaxLength`, `Required`, `NotMapped`, `DatabaseGenerated`)
13+
- Columns from properties (supported annotations: `Column`, `Key`, `MaxLength`, `Required`, `NotMapped`, `DatabaseGenerated`, `Index`)
1414
- PrimaryKey constraint (`Key` annotation, key composites are supported)
1515
- ForeignKey constraint (1-n relationships, support for 'Cascade on delete')
1616
- Not Null constraint
1717
- Auto increment (An int PrimaryKey will automatically be incremented)
18+
- 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`)
1819

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

25-
## How to use
26-
Either get the assembly from the latest [release](https://github.com/msallin/SQLiteCodeFirst/releases) or install the NuGet-Package [SQLite.CodeFirst](https://www.nuget.org/packages/SQLite.CodeFirst/).
26+
## Install
27+
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`).
28+
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:
31+
- .NET 4.0 (use net40)
32+
- .NET 4.5 (use net45)
33+
- .NET 4.5.1 (use net45)
34+
- .NET 4.5.2 (use net45)
2735

28-
If you want to let the Entity Framework create the database, if it does not exist, just set `SqliteDropCreateDatabaseAlways<>` or `SqliteCreateDatabaseIfNotExists<>` as your `IDbInitializer<>`.
36+
## How to use
37+
When you want to let the Entity Framework create database if it does not exist, just set `SqliteDropCreateDatabaseAlways<>` or `SqliteCreateDatabaseIfNotExists<>` as your `IDbInitializer<>`.
2938
```csharp
3039
public class MyDbContext : DbContext
3140
{
@@ -34,21 +43,20 @@ public class MyDbContext : DbContext
3443

3544
protected override void OnModelCreating(DbModelBuilder modelBuilder)
3645
{
37-
var sqliteConnectionInitializer = new SqliteCreateDatabaseIfNotExists<MyDbContext>(
38-
Database.Connection.ConnectionString, modelBuilder);
46+
var sqliteConnectionInitializer = new SqliteCreateDatabaseIfNotExists<MyDbContext>(modelBuilder);
3947
Database.SetInitializer(sqliteConnectionInitializer);
4048
}
4149
}
4250
```
4351

44-
In a more advanced scenario you may want to populate some core- or test-data after the database was created.
52+
In a more advanced scenario, you may want to populate some core- or test-data after the database was created.
4553
To do this, inherit from `SqliteDropCreateDatabaseAlways<>` or `SqliteCreateDatabaseIfNotExists<>` and override the `Seed(MyDbContext context)` function.
46-
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.
54+
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.
4755
```csharp
48-
public class MyDbContextContextInitializer : SqliteDropCreateDatabaseAlways<MyDbContext>
56+
public class MyDbContextInitializer : SqliteDropCreateDatabaseAlways<MyDbContext>
4957
{
50-
public MyDbContextInitializer(string connectionString, DbModelBuilder modelBuilder)
51-
: base(connectionString, modelBuilder) { }
58+
public MyDbContextInitializer(DbModelBuilder modelBuilder)
59+
: base(modelBuilder) { }
5260

5361
protected override void Seed(MyDbContext context)
5462
{
@@ -58,5 +66,9 @@ public class MyDbContextContextInitializer : SqliteDropCreateDatabaseAlways<MyDb
5866
```
5967

6068
## Hints
69+
<<<<<<< HEAD
6170
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.
71+
=======
72+
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.
73+
>>>>>>> refs/remotes/msallin/master
6274
In this case please check the following issue: https://github.com/msallin/SQLiteCodeFirst/issues/13.

SQLite.CodeFirst.Console/App.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
55
</configSections>
66
<connectionStrings>
7-
<add name="footballDb" connectionString="data source=.\footballDb.sqlite" providerName="System.Data.SQLite" />
7+
<add name="footballDb" connectionString="data source=.\db\footballDb\footballDb.sqlite;foreign keys=true" providerName="System.Data.SQLite" />
88
</connectionStrings>
99
<startup>
1010
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace SQLite.CodeFirst.Console.Entity
2+
{
3+
public class Coach : Person
4+
{
5+
public virtual Team Team { get; set; }
6+
}
7+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System.ComponentModel.DataAnnotations;
2+
3+
namespace SQLite.CodeFirst.Console.Entity
4+
{
5+
public abstract class Person : IEntity
6+
{
7+
public int Id { get; set; }
8+
9+
[MaxLength(50)]
10+
public string FirstName { get; set; }
11+
12+
[MaxLength(50)]
13+
public string LastName { get; set; }
14+
15+
[MaxLength(100)]
16+
public string Street { get; set; }
17+
18+
[Required]
19+
public string City { get; set; }
20+
}
21+
}

SQLite.CodeFirst.Console/Entity/Player.cs

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,12 @@
1-
using System.ComponentModel.DataAnnotations;
2-
using System.ComponentModel.DataAnnotations.Schema;
1+
using System.ComponentModel.DataAnnotations.Schema;
32

43
namespace SQLite.CodeFirst.Console.Entity
54
{
65
[Table("TeamPlayer")]
7-
public class Player : IEntity
6+
public class Player : Person
87
{
9-
public int Id { get; set; }
10-
11-
[MaxLength(50)]
12-
public string FirstName { get; set; }
13-
14-
[MaxLength(50)]
15-
public string LastName { get; set; }
16-
17-
[MaxLength(100)]
18-
public string Street { get; set; }
19-
20-
[Required]
21-
public string City { get; set; }
8+
[Index] // Automatically named 'IX_TeamPlayer_Number'
9+
public int Number { get; set; }
2210

2311
public virtual Team Team { get; set; }
2412
}

SQLite.CodeFirst.Console/Entity/Stadion.cs

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

1213
[Key]
1314
[Column(Order = 2)]
15+
[Index("IX_Stadion_Main", Order = 1)] // Test for combined, named index
1416
public string Street { get; set; }
1517

1618
[Key]

SQLite.CodeFirst.Console/Entity/Team.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
using System.Collections.Generic;
22
using System.ComponentModel.DataAnnotations;
3+
using System.ComponentModel.DataAnnotations.Schema;
34

45
namespace SQLite.CodeFirst.Console.Entity
56
{
67
public class Team : IEntity
78
{
89
public int Id { get; set; }
910

11+
[Index("IX_Team_TeamsName")] // Test for named index.
1012
[Required]
1113
public string Name { get; set; }
1214

15+
public virtual Coach Coach { get; set; }
16+
1317
public virtual ICollection<Player> Players { get; set; }
1418

1519
public virtual Stadion Stadion { get; set; }

0 commit comments

Comments
 (0)