-
Notifications
You must be signed in to change notification settings - Fork 0
/
DatabaseContext.cs
35 lines (31 loc) · 1016 Bytes
/
DatabaseContext.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
using Microsoft.EntityFrameworkCore;
using TestEF.Entities;
namespace TestEF
{
public class DatabaseContext(DbContextOptions options) : DbContext(options)
{
public DbSet<ChatEntity> Chats { get; set; }
public DbSet<MessageEntity> Messages { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder
.Entity<ChatEntity>()
.ToTable("Chats");
modelBuilder
.Entity<ChatEntity>()
.HasKey(x => x.Uid);
modelBuilder
.Entity<MessageEntity>()
.ToTable("Messages");
modelBuilder
.Entity<MessageEntity>()
.HasKey(x => x.Uid);
modelBuilder
.Entity<MessageEntity>()
.HasOne(x => x.Chat)
.WithMany(y => y.Messages)
.HasForeignKey(x => x.ChatUid)
.IsRequired();
}
}
}