bulletboards/ImageBoardServerApp/Data/AppDBContext.cs

36 lines
No EOL
1.2 KiB
C#

using Microsoft.EntityFrameworkCore;
namespace ImageBoardServerApp.Data;
internal sealed class AppDBContext : DbContext
{
public DbSet<UserData> Users { get; set; }
public DbSet<PostData> Posts { get; set; }
public DbSet<ImageData> Images { get; set; }
public DbSet<CommentData> Comments { get; set; }
public DbSet<AccountData> Accounts { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder.UseSqlite("Data Source=./Data/Nils.db");
protected override void OnModelCreating(ModelBuilder mb)
{
mb.Entity<PostData>()
.HasMany(post => post.Comments)
.WithOne(comment => comment.Post);
mb.Entity<PostData>()
.HasOne(post => post.Image)
.WithOne(image => image.Post);
mb.Entity<CommentData>()
.HasOne(comment => comment.Image)
.WithOne(image => image.Comment);
mb.Entity<UserData>()
.HasMany(user => user.Posts)
.WithOne(post => post.User);
mb.Entity<UserData>()
.HasMany(user => user.Comments)
.WithOne(comment => comment.User);
}
}