-
Notifications
You must be signed in to change notification settings - Fork 0
/
VBDbContext.cs
104 lines (86 loc) · 4.29 KB
/
VBDbContext.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
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
using Microsoft.EntityFrameworkCore;
using System;
using ULabs.VBulletinEntity.Models.AddOns;
using ULabs.VBulletinEntity.Models.Config;
using ULabs.VBulletinEntity.Models.Forum;
using ULabs.VBulletinEntity.Models.Message;
using ULabs.VBulletinEntity.Models.User;
namespace ULabs.VBulletinEntity {
public class VBDbContext : DbContext {
public VBDbContext(DbContextOptions options) : base(options) { }
public DbSet<VBUser> Users { get; set; }
public DbSet<VBUserGroup> UserGroups { get; set; }
public DbSet<VBSession> Sessions { get; set; }
public DbSet<VBCustomAvatar> CustomAvatars { get; set; }
public DbSet<VBPost> Posts { get; set; }
public DbSet<VBThread> Threads { get; set; }
public DbSet<VBForum> Forums { get; set; }
public DbSet<VBForumPermission> ForumPermissions { get; set; }
public DbSet<VBPoll> Polls { get; set; }
public DbSet<VBMessage> Messages { get; set; }
public DbSet<VBMessageText> MessagesText { get; set; }
public DbSet<VBAttachment> Attachments { get; set; }
public DbSet<VBThreadRead> ThreadReads { get; set; }
public DbSet<VBSettings> Settings { get; set; }
public DbSet<PostThanks> PostThanks { get; set; }
protected override void OnModelCreating(ModelBuilder builder) {
// Since vB has multiple FKs to VBPost (e.g. first/last post) we need to tell EF which of them is used for our 1:n mapping (thread/replys)
builder.Entity<VBPost>()
.HasOne(p => p.Thread)
.WithMany(t => t.Replys)
.HasForeignKey(x => x.ThreadId);
builder.Entity<VBPost>()
.HasOne(p => p.Author)
.WithMany(u => u.Posts)
.HasForeignKey(p => p.AuthorId);
// UserId zero got set when deleting user
builder.Entity<VBPost>()
.Property(p => p.AuthorId)
.HasDefaultValue(0);
// Specify polls as optional so that thread doesn't got null when it has no poll
builder.Entity<VBThread>()
.HasOne(t => t.Poll)
.WithMany()
.HasForeignKey(t => t.PollId);
// Solves the problem that a poll can be optional but vB doesn't allow null values here (only decimal 0) which cause conflicts to EF behavior.
// The trick is: Make PollId nullable and set default value to decimal 0. So EF use 0 instead of null.
builder.Entity<VBThread>()
.Property(t => t.PollId)
.HasDefaultValue(0);
builder.Entity<VBThread>()
.HasOne(t => t.FirstPost)
.WithMany()
.HasForeignKey(t => t.FirstPostId);
builder.Entity<VBThreadRead>()
.HasKey(r => new { r.ThreadId, r.UserId });
builder.Entity<VBThread>()
.HasOne(t => t.LastPost)
.WithMany()
.HasForeignKey(t => t.LastPostId);
builder.Entity<VBPost>()
.HasMany(p => p.Attachments)
.WithOne()
.HasForeignKey(attach => attach.ContentId);
builder.Entity<VBForum>()
.HasMany(f => f.Permissions)
//.WithOne(perm => perm.Forum)
.WithOne()
.HasForeignKey(p => p.ForumId);
builder.Entity<VBForum>()
.HasOne(f => f.Parent)
.WithOne()
.HasForeignKey<VBForum>(f => f.ParentId);
builder.Entity<VBForum>()
.Property(f => f.ParentId)
.HasDefaultValue(-1);
// Define custom avatar optional. Per default it would be required so that no posts were loaded when their authors haven't a custom avatar
builder.Entity<VBUser>()
.HasOne(u => u.CustomAvatar)
.WithOne()
.HasForeignKey<VBCustomAvatar>(a => a.UserId);
// ToDo: Most examples in this guide show configurations being applied in the OnModelCreating method, but it is recommended to separate configurations out to individual files per entity
// https://www.learnentityframeworkcore.com/configuration/fluent-api
base.OnModelCreating(builder);
}
}
}