-
Notifications
You must be signed in to change notification settings - Fork 0
/
HistoryDb.cs
46 lines (40 loc) · 1.25 KB
/
HistoryDb.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
using Microsoft.EntityFrameworkCore;
public class EnrollmentHistoryContext : DbContext
{
public DbSet<CourseSection> Snapshots => Set<CourseSection>();
protected override void OnConfiguring(DbContextOptionsBuilder builder)
{
// TODO switch to SecretsManager
builder.UseMySQL(Environment.GetEnvironmentVariable("RDS_ACCESS_CREDENTIALS")!);
}
protected override void OnModelCreating(ModelBuilder builder)
{
builder.Entity<CourseSection>()
.ToTable("SnapshotsCompressed");
builder.Entity<CourseSection>()
.HasKey(c => new { c.ClassNumber, c.Time });
builder.Entity<CourseSection>()
.Property(c => c.CourseCode)
.HasColumnType("varchar(20)");
builder.Entity<CourseSection>()
.HasIndex(c => c.CourseCode);
}
}
public class CourseDatabase
{
public CourseDatabase()
{
using (var ctx = new EnrollmentHistoryContext())
{
ctx.Database.EnsureCreated();
}
}
public async Task SaveCourses(IEnumerable<CourseSection> sections)
{
using (var ctx = new EnrollmentHistoryContext())
{
ctx.Snapshots.AddRange(sections);
await ctx.SaveChangesAsync();
}
}
}