forked from dotnet-presentations/aspnetcore-app-workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApplicationDbContext.cs
51 lines (38 loc) · 1.56 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
using Microsoft.EntityFrameworkCore;
namespace BackEnd.Data
{
public class ApplicationDbContext : DbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Attendee>()
.HasIndex(a => a.UserName)
.IsUnique();
// Ignore the computed property
modelBuilder.Entity<Session>()
.Ignore(s => s.Duration);
// Many-to-many: Conference <-> Attendee
modelBuilder.Entity<ConferenceAttendee>()
.HasKey(ca => new { ca.ConferenceID, ca.AttendeeID });
// Many-to-many: Session <-> Attendee
modelBuilder.Entity<SessionAttendee>()
.HasKey(ca => new { ca.SessionID, ca.AttendeeID });
// Many-to-many: Speaker <-> Session
modelBuilder.Entity<SessionSpeaker>()
.HasKey(ss => new { ss.SessionId, ss.SpeakerId });
// Many-to-many: Session <-> Tag
modelBuilder.Entity<SessionTag>()
.HasKey(st => new { st.SessionID, st.TagID });
}
public DbSet<Conference> Conferences { get; set; }
public DbSet<Session> Sessions { get; set; }
public DbSet<Track> Tracks { get; set; }
public DbSet<Tag> Tags { get; set; }
public DbSet<Speaker> Speakers { get; set; }
public DbSet<Attendee> Attendees { get; set; }
}
}