forked from msallin/SQLiteCodeFirst
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
146 lines (129 loc) · 5.12 KB
/
Program.cs
File metadata and controls
146 lines (129 loc) · 5.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.SQLite;
using System.Linq;
using SQLite.CodeFirst.Console.Entity;
namespace SQLite.CodeFirst.Console
{
public static class Program
{
private static void Main()
{
StartDemoUseInMemory();
StartDemoUseFile();
PressEnterToExit();
}
private static void StartDemoUseInMemory()
{
System.Console.WriteLine("Starting Demo Application (In Memory)");
System.Console.WriteLine(string.Empty);
using (var sqLiteConnection = new SQLiteConnection("data source=:memory:"))
{
// This is required if a in memory db is used.
sqLiteConnection.Open();
using (var context = new FootballDbContext(sqLiteConnection, false))
{
CreateAndSeedDatabase(context);
DisplaySeededData(context);
}
}
}
private static void StartDemoUseFile()
{
System.Console.WriteLine("Starting Demo Application (File)");
System.Console.WriteLine(string.Empty);
using (var context = new FootballDbContext("footballDb"))
{
CreateAndSeedDatabase(context);
DisplaySeededData(context);
}
}
private static void CreateAndSeedDatabase(DbContext context)
{
System.Console.WriteLine("Create and seed the database.");
if (context.Set<Team>().Count() != 0)
{
return;
}
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"
}
});
context.SaveChanges();
System.Console.WriteLine("Completed.");
System.Console.WriteLine();
}
private static void DisplaySeededData(DbContext context)
{
System.Console.WriteLine("Display seeded data.");
foreach (Team team in context.Set<Team>())
{
System.Console.WriteLine("\t Team:");
System.Console.WriteLine("\t Id: {0}", team.Id);
System.Console.WriteLine("\t Name: {0}", team.Name);
System.Console.WriteLine();
System.Console.WriteLine("\t\t Stadion:");
System.Console.WriteLine("\t\t Name: {0}", team.Stadion.Name);
System.Console.WriteLine("\t\t Street: {0}", team.Stadion.Street);
System.Console.WriteLine("\t\t City: {0}", team.Stadion.City);
System.Console.WriteLine();
System.Console.WriteLine("\t\t Coach:");
System.Console.WriteLine("\t\t Id: {0}", team.Coach.Id);
System.Console.WriteLine("\t\t FirstName: {0}", team.Coach.FirstName);
System.Console.WriteLine("\t\t LastName: {0}", team.Coach.LastName);
System.Console.WriteLine("\t\t Street: {0}", team.Coach.Street);
System.Console.WriteLine("\t\t City: {0}", team.Coach.City);
System.Console.WriteLine();
foreach (Player player in team.Players)
{
System.Console.WriteLine("\t\t Player:");
System.Console.WriteLine("\t\t Id: {0}", player.Id);
System.Console.WriteLine("\t\t Number: {0}", player.Number);
System.Console.WriteLine("\t\t FirstName: {0}", player.FirstName);
System.Console.WriteLine("\t\t LastName: {0}", player.LastName);
System.Console.WriteLine("\t\t Street: {0}", player.Street);
System.Console.WriteLine("\t\t City: {0}", player.City);
System.Console.WriteLine();
}
}
}
private static void PressEnterToExit()
{
System.Console.WriteLine();
System.Console.WriteLine("Press 'Enter' to exit.");
System.Console.ReadLine();
}
}
}