-
Notifications
You must be signed in to change notification settings - Fork 0
/
DbContextExtension.cs
71 lines (63 loc) · 2.05 KB
/
DbContextExtension.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using System.IO;
using Newtonsoft.Json;
using MyWeeFee.Models;
namespace MyWeeFee
{
public static class DbContextExtension
{
/*
public static bool AllMigrationsApplied(this DbContext db)
{
var applied = db.GetService<IHistoryRepository>()
.GetAppliedMigrations()
.Select(m => m.MigrationId);
var total = db.GetService<IMigrationsAssembly>()
.Migrations
.Select(m => m.Key);
return !total.Except(applied).Any();
}
*/
// ALTERNATIVELY (with JSON)
/*
public static void EnsureSeeded(this MyWeeFeeContext db)
{
// Ensure we have some APEncryptions
if (!db.T_APEncryptions.Any())
{
var ape = JsonConvert.DeserializeObject<List<APEncryption>>(File.ReadAllText(@"Seed" + Path.DirectorySeparatorChar + "APEncryptions.json"));
db.T_APEncryptions.AddRange(ape);
db.SaveChanges();
}
}
*/
// ALTERNATIVELY (without JSON)
/*
public static void EnsureSeeded(this MyWeeFeeContext db)
{
AddAPEncryption(db);
}
private static async void AddAPEncryption(MyWeeFeeContext db)
{
if (db.T_APEncryptions.Any())
{
return; // DB has been seeded
}
db.T_APEncryptions.AddRange(
new APEncryption { Id = 0, Encryption = "Ohne (Offen)" },
new APEncryption { Id = 1, Encryption = "WEP" },
new APEncryption { Id = 2, Encryption = "WPA" },
new APEncryption { Id = 3, Encryption = "WPA2 (AES)" },
new APEncryption { Id = 4, Encryption = "WPA2 (TKIP)" }
);
db.SaveChanges();
}
*/
}
}