-
-
Notifications
You must be signed in to change notification settings - Fork 158
/
Copy pathAppDbContext.cs
62 lines (52 loc) · 2.37 KB
/
AppDbContext.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
using JsonApiDotNetCoreExample.Models;
using Microsoft.EntityFrameworkCore;
using JsonApiDotNetCoreExample.Models.Entities;
namespace JsonApiDotNetCoreExample.Data
{
public class AppDbContext : DbContext
{
public AppDbContext(DbContextOptions<AppDbContext> options)
: base(options)
{ }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<TodoItem>()
.Property(t => t.CreatedDate).HasDefaultValueSql("CURRENT_TIMESTAMP").IsRequired();
modelBuilder.Entity<TodoItem>()
.HasOne(t => t.Assignee)
.WithMany(p => p.AssignedTodoItems)
.HasForeignKey(t => t.AssigneeId);
modelBuilder.Entity<TodoItem>()
.HasOne(t => t.Owner)
.WithMany(p => p.TodoItems)
.HasForeignKey(t => t.OwnerId);
modelBuilder.Entity<CourseStudentEntity>()
.HasKey(r => new { r.CourseId, r.StudentId });
modelBuilder.Entity<CourseStudentEntity>()
.HasOne(r => r.Course)
.WithMany(c => c.Students)
.HasForeignKey(r => r.CourseId);
modelBuilder.Entity<CourseStudentEntity>()
.HasOne(r => r.Student)
.WithMany(s => s.Courses)
.HasForeignKey(r => r.StudentId);
modelBuilder.Entity<ArticleTag>()
.HasKey(bc => new { bc.ArticleId, bc.TagId });
}
public DbSet<TodoItem> TodoItems { get; set; }
public DbSet<Person> People { get; set; }
public DbSet<TodoItemCollection> TodoItemCollections { get; set; }
public DbSet<CamelCasedModel> CamelCasedModels { get; set; }
public DbSet<Article> Articles { get; set; }
public DbSet<Author> Authors { get; set; }
public DbSet<NonJsonApiResource> NonJsonApiResources { get; set; }
public DbSet<User> Users { get; set; }
public DbSet<CourseEntity> Courses { get; set; }
public DbSet<DepartmentEntity> Departments { get; set; }
public DbSet<CourseStudentEntity> Registrations { get; set; }
public DbSet<StudentEntity> Students { get; set; }
public DbSet<PersonRole> PersonRoles { get; set; }
public DbSet<ArticleTag> ArticleTags { get; set; }
public DbSet<Tag> Tags { get; set; }
}
}