-
Notifications
You must be signed in to change notification settings - Fork 159
/
Copy pathApplicationDbContext.cs
74 lines (57 loc) · 1.7 KB
/
ApplicationDbContext.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
72
73
74
using CoreWiki.Data.EntityFramework.Models;
using Microsoft.EntityFrameworkCore;
using NodaTime;
using System;
using System.Threading;
using System.Threading.Tasks;
namespace CoreWiki.Data.EntityFramework
{
public class ApplicationDbContext : DbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
var homePage = new ArticleDAO
{
Id = 1,
Topic = "Home Page",
Slug = "home-page",
Content = "This is the default home page. Please change me!",
Published = Instant.FromDateTimeUtc(new DateTime(2018, 6, 19, 14, 31, 2, 265, DateTimeKind.Utc)),
AuthorId = Guid.Empty
};
var homePageHistory = ArticleHistoryDAO.FromArticle(homePage);
homePageHistory.Id = 1;
homePageHistory.Article = null;
modelBuilder.Entity<ArticleDAO>(entity =>
{
entity.HasIndex(a => a.Slug).IsUnique();
entity.HasData(homePage);
});
modelBuilder.Entity<ArticleHistoryDAO>(entity =>
{
entity.HasData(homePageHistory);
});
modelBuilder.Entity<SlugHistoryDAO>(entity =>
{
entity.HasIndex(a => new { a.OldSlug, a.AddedDateTime });
});
}
public DbSet<ArticleDAO> Articles { get; set; }
public DbSet<CommentDAO> Comments { get; set; }
public DbSet<SlugHistoryDAO> SlugHistories { get; set; }
public DbSet<ArticleHistoryDAO> ArticleHistories { get; set; }
public override Task<int> SaveChangesAsync(
CancellationToken cancellationToken = default(CancellationToken))
{
return base.SaveChangesAsync(cancellationToken);
}
public static void SeedData(ApplicationDbContext context)
{
context.Database.Migrate();
}
}
}