Skip to content

Commit a5eb90f

Browse files
committed
Enhanced Demo application.
1 parent 22411d3 commit a5eb90f

File tree

7 files changed

+174
-64
lines changed

7 files changed

+174
-64
lines changed

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="test" connectionString="data source=C:\temp\testdb.sqlite" providerName="System.Data.SQLite" />
7+
<add name="footballDb" connectionString="data source=.\footballDb.sqlite" providerName="System.Data.SQLite" />
88
</connectionStrings>
99
<startup>
1010
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />

SQLite.CodeFirst.Console/Entity/Player.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,23 @@
33

44
namespace SQLite.CodeFirst.Console.Entity
55
{
6-
public class Player
6+
[Table("TeamPlayer")]
7+
public class Player : IEntity
78
{
8-
[Key]
9-
[Column(Order = 1)]
9+
public int Id { get; set; }
10+
11+
[MaxLength(50)]
1012
public string FirstName { get; set; }
11-
[Key]
12-
[Column(Order = 2)]
13+
14+
[MaxLength(50)]
1315
public string LastName { get; set; }
16+
1417
[MaxLength(100)]
1518
public string Street { get; set; }
19+
1620
[Required]
1721
public string City { get; set; }
18-
public Team Team { get; set; }
22+
23+
public virtual Team Team { get; set; }
1924
}
2025
}
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
using System.Collections.Generic;
2+
using System.ComponentModel.DataAnnotations;
23

34
namespace SQLite.CodeFirst.Console.Entity
45
{
56
public class Team : IEntity
67
{
78
public int Id { get; set; }
8-
public ICollection<Player> Players { get; set; }
9+
10+
[Required]
11+
public string Name { get; set; }
12+
13+
public virtual ICollection<Player> Players { get; set; }
14+
15+
public virtual Stadion Stadion { get; set; }
916
}
1017
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
using System.Collections.Generic;
2+
using System.Data.Entity;
3+
using System.Data.Entity.ModelConfiguration.Conventions;
4+
using SQLite.CodeFirst.Console.Entity;
5+
6+
namespace SQLite.CodeFirst.Console
7+
{
8+
public class FootballDbContext : DbContext
9+
{
10+
public FootballDbContext()
11+
: base("footballDb")
12+
{
13+
Configuration.ProxyCreationEnabled = true;
14+
Configuration.LazyLoadingEnabled = true;
15+
}
16+
17+
protected override void OnModelCreating(DbModelBuilder modelBuilder)
18+
{
19+
ConfigureTeamEntity(modelBuilder);
20+
ConfigureStadionEntity(modelBuilder);
21+
ConfigurePlayerEntity(modelBuilder);
22+
23+
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
24+
25+
var sqliteConnectionInitializer = new TestDbContextInitializer(Database.Connection.ConnectionString, modelBuilder);
26+
Database.SetInitializer(sqliteConnectionInitializer);
27+
}
28+
29+
private static void ConfigureTeamEntity(DbModelBuilder modelBuilder)
30+
{
31+
modelBuilder.Entity<Team>();
32+
}
33+
34+
private static void ConfigureStadionEntity(DbModelBuilder modelBuilder)
35+
{
36+
modelBuilder.Entity<Stadion>();
37+
}
38+
39+
private static void ConfigurePlayerEntity(DbModelBuilder modelBuilder)
40+
{
41+
modelBuilder.Entity<Player>()
42+
.HasRequired(p => p.Team)
43+
.WithMany(team => team.Players)
44+
.WillCascadeOnDelete(true);
45+
}
46+
}
47+
48+
public class TestDbContextInitializer : SqliteContextInitializer<FootballDbContext>
49+
{
50+
public TestDbContextInitializer(string connectionString, DbModelBuilder modelBuilder)
51+
: base(connectionString, modelBuilder) { }
52+
53+
protected override void Seed(FootballDbContext context)
54+
{
55+
context.Set<Team>().Add(new Team
56+
{
57+
Name = "YB",
58+
Players = new List<Player>
59+
{
60+
new Player
61+
{
62+
City = "Bern",
63+
FirstName = "Marco",
64+
LastName = "Bürki",
65+
Street = "Wunderstrasse 43"
66+
},
67+
new Player
68+
{
69+
City = "Berlin",
70+
FirstName = "Alain",
71+
LastName = "Rochat",
72+
Street = "Wonderstreet 13"
73+
}
74+
},
75+
Stadion = new Stadion
76+
{
77+
Name = "Stade de Suisse",
78+
City = "Bern",
79+
Street = "Papiermühlestrasse 71"
80+
}
81+
});
82+
}
83+
}
84+
}

SQLite.CodeFirst.Console/Program.cs

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.Linq;
1+
using System.IO;
22
using SQLite.CodeFirst.Console.Entity;
33

44
namespace SQLite.CodeFirst.Console
@@ -7,8 +7,72 @@ public static class Program
77
{
88
static void Main()
99
{
10-
var context = new TestDbContext();
11-
System.Console.WriteLine(context.Set<Player>().Count());
10+
System.Console.WriteLine("Starting Demo Application");
11+
12+
ClearExistingDbFile();
13+
14+
var context = CreateAndSeedDatabase();
15+
16+
DisplaySeededData(context);
17+
18+
PressEnterToExit();
19+
}
20+
21+
private static void ClearExistingDbFile()
22+
{
23+
System.Console.WriteLine("Check for existing db file.");
24+
const string SqliteFilePath = @".\footballDb.sqlite";
25+
if (File.Exists(SqliteFilePath))
26+
{
27+
System.Console.WriteLine("Delete existing db file.");
28+
File.Delete(SqliteFilePath);
29+
}
30+
System.Console.WriteLine();
31+
}
32+
33+
private static FootballDbContext CreateAndSeedDatabase()
34+
{
35+
System.Console.WriteLine("Create and seed the database.");
36+
var context = new FootballDbContext();
37+
System.Console.WriteLine("Completed.");
38+
System.Console.WriteLine();
39+
return context;
40+
}
41+
42+
private static void DisplaySeededData(FootballDbContext context)
43+
{
44+
System.Console.WriteLine("Display seeded data.");
45+
46+
foreach (var team in context.Set<Team>())
47+
{
48+
System.Console.WriteLine("\t Team:");
49+
System.Console.WriteLine("\t Id: {0}", team.Id);
50+
System.Console.WriteLine("\t Name: {0}", team.Name);
51+
System.Console.WriteLine();
52+
53+
System.Console.WriteLine("\t\t Stadion:");
54+
System.Console.WriteLine("\t\t Name: {0}", team.Stadion.Name);
55+
System.Console.WriteLine("\t\t Street: {0}", team.Stadion.Street);
56+
System.Console.WriteLine("\t\t City: {0}", team.Stadion.City);
57+
System.Console.WriteLine();
58+
59+
foreach (var player in team.Players)
60+
{
61+
System.Console.WriteLine("\t\t Player:");
62+
System.Console.WriteLine("\t\t Id: {0}", player.Id);
63+
System.Console.WriteLine("\t\t FirstName: {0}", player.FirstName);
64+
System.Console.WriteLine("\t\t LastName: {0}", player.LastName);
65+
System.Console.WriteLine("\t\t Street: {0}", player.Street);
66+
System.Console.WriteLine("\t\t City: {0}", player.City);
67+
System.Console.WriteLine();
68+
}
69+
}
70+
}
71+
72+
private static void PressEnterToExit()
73+
{
74+
System.Console.WriteLine();
75+
System.Console.WriteLine("Press 'Enter' to exit.");
1276
System.Console.ReadLine();
1377
}
1478
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
</Reference>
4545
<Reference Include="System" />
4646
<Reference Include="System.ComponentModel.DataAnnotations" />
47+
<Reference Include="System.Configuration" />
4748
<Reference Include="System.Core" />
4849
<Reference Include="System.Data.SQLite">
4950
<HintPath>..\packages\System.Data.SQLite.Core.1.0.96.0\lib\net451\System.Data.SQLite.dll</HintPath>
@@ -63,8 +64,9 @@
6364
<ItemGroup>
6465
<Compile Include="Entity\IEntity.cs" />
6566
<Compile Include="Entity\Player.cs" />
67+
<Compile Include="Entity\Stadion.cs" />
6668
<Compile Include="Entity\Team.cs" />
67-
<Compile Include="TestDbContext.cs" />
69+
<Compile Include="FootballDbContext.cs" />
6870
<Compile Include="Program.cs" />
6971
<Compile Include="Properties\AssemblyInfo.cs" />
7072
</ItemGroup>

SQLite.CodeFirst.Console/TestDbContext.cs

Lines changed: 0 additions & 52 deletions
This file was deleted.

0 commit comments

Comments
 (0)