-
-
Notifications
You must be signed in to change notification settings - Fork 163
Expand file tree
/
Copy pathAppDbContext.cs
More file actions
68 lines (57 loc) · 2.26 KB
/
AppDbContext.cs
File metadata and controls
68 lines (57 loc) · 2.26 KB
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
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 { get; set; }
public DbSet<Person> People { get; set; }
public DbSet<Article> Articles { get; set; }
public DbSet<Author> AuthorDifferentDbContextName { get; set; }
public DbSet<User> Users { get; set; }
public AppDbContext(DbContextOptions<AppDbContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
builder.Entity<TodoItem>()
.HasOne(todoItem => todoItem.Assignee)
.WithMany(person => person.AssignedTodoItems);
builder.Entity<TodoItem>()
.HasOne(todoItem => todoItem.Owner)
.WithMany(person => person.TodoItems);
builder.Entity<ArticleTag>()
.HasKey(bc => new
{
bc.ArticleId,
bc.TagId
});
builder.Entity<IdentifiableArticleTag>()
.HasKey(bc => new
{
bc.ArticleId,
bc.TagId
});
builder.Entity<Person>()
.HasOne(person => person.StakeHolderTodoItem)
.WithMany(todoItem => todoItem.StakeHolders)
.OnDelete(DeleteBehavior.Cascade);
builder.Entity<TodoItem>()
.HasMany(todoItem => todoItem.ChildTodoItems)
.WithOne(todoItem => todoItem.ParentTodo);
builder.Entity<Passport>()
.HasOne(passport => passport.Person)
.WithOne(person => person.Passport)
.HasForeignKey<Person>("PassportKey")
.OnDelete(DeleteBehavior.SetNull);
builder.Entity<TodoItem>()
.HasOne(todoItem => todoItem.OneToOnePerson)
.WithOne(person => person.OneToOneTodoItem)
.HasForeignKey<TodoItem>("OneToOnePersonKey");
}
}
}