-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNoteDbContext.cs
More file actions
31 lines (26 loc) · 1.02 KB
/
NoteDbContext.cs
File metadata and controls
31 lines (26 loc) · 1.02 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
using Microsoft.EntityFrameworkCore;
using SocialAPI.Models;
namespace SocialAPI;
public class NoteDbContext(DbContextOptions<NoteDbContext> options) : DbContext(options)
{
public DbSet<Note> Notes { get; set; }
public DbSet<NoteTag> NoteTags { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseNpgsql(
"Server=localhost;Port=5432;Database=c_sharp_notes;User ID=c_sharp_notes;Password=cSharp;",
options => options.UseAdminDatabase("c_sharp_notes"));
base.OnConfiguring(optionsBuilder);
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Note>()
.Property(n => n.CreatedAt)
.HasDefaultValueSql("CURRENT_TIMESTAMP")
.ValueGeneratedOnAdd();
modelBuilder.Entity<Note>()
.Property(n => n.UpdatedAt)
.HasDefaultValueSql("CURRENT_TIMESTAMP")
.ValueGeneratedOnAddOrUpdate();
}
}