-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathProgram.cs
More file actions
56 lines (52 loc) · 2.72 KB
/
Program.cs
File metadata and controls
56 lines (52 loc) · 2.72 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
using System;
using System.IO;
using System.Linq;
namespace ConsoleApp.SQLite
{
public class Program
{
public static void Main()
{
// Tipp: SQLite Cipher databases can be created/explored manually using https://sqlitebrowser.org/ (or on GitHub https://github.com/sqlitebrowser/sqlitebrowser)
const string newDatabaseFile = "newblogging.db";
foreach (var databaseFile in File.Exists(BloggingContext.DEFAULTDBFILE)
? new[] { BloggingContext.DEFAULTDBFILE, newDatabaseFile }// If the default existing database exists
: new[] { newDatabaseFile, BloggingContext.DEFAULTDBFILE }// The default existing database will be a copy of the new database from the first test
)
{
// Prepare the test
if (databaseFile == BloggingContext.DEFAULTDBFILE)
{
// Use the existing database
if (!File.Exists(databaseFile))
{
// Copy the previously created new database as default existing database
Console.WriteLine("Use \"{0}\" as default existing database \"{1}\"", newDatabaseFile, databaseFile);
File.Copy(newDatabaseFile, databaseFile);
}
Console.WriteLine("Update existing database \"{0}\"", databaseFile);
}
else
{
// Create a new database
if (File.Exists(databaseFile))
{
// Delete the previously created database (ensure we're creating a new database)
Console.WriteLine("Delete existing database \"{0}\"", databaseFile);
File.Delete(databaseFile);
}
Console.WriteLine("Test creating new database \"{0}\"", databaseFile);
}
// Initialize the database context (ensure the database is created, if it's a new database)
using var db = new BloggingContext(databaseFile);
if (databaseFile == newDatabaseFile) db.Database.EnsureCreated();
// Add a blog URI
db.Blogs.Add(new Blog { Url = "http://blogs.msdn.com/adonet" });
Console.WriteLine("{0} records saved to database", db.SaveChanges());
// Display all blog URIs from the current database
Console.WriteLine("All {0} blogs in database:", db.Blogs.Count());
foreach (var blog in db.Blogs) Console.WriteLine(" - #{0} {1}", blog.BlogId, blog.Url);
}
}
}
}