-
-
Notifications
You must be signed in to change notification settings - Fork 158
/
Copy pathAppDbContext.cs
32 lines (26 loc) · 998 Bytes
/
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
using JetBrains.Annotations;
using JsonApiDotNetCoreExample.Models;
using Microsoft.EntityFrameworkCore;
// @formatter:wrap_chained_method_calls chop_always
namespace JsonApiDotNetCoreExample.Data;
[UsedImplicitly(ImplicitUseTargetFlags.Members)]
public sealed class AppDbContext : DbContext
{
public DbSet<TodoItem> TodoItems => Set<TodoItem>();
public DbSet<PostIt> PostIts => Set<PostIt>();
public AppDbContext(DbContextOptions<AppDbContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
// When deleting a person, un-assign him/her from existing todo items.
builder.Entity<Person>()
.HasMany(person => person.AssignedTodoItems)
.WithOne(todoItem => todoItem.Assignee!);
// When deleting a person, the todo items he/she owns are deleted too.
builder.Entity<TodoItem>()
.HasOne(todoItem => todoItem.Owner)
.WithMany();
}
}