bulletboards/ImageBoardServerApp/Data/AppDBContext.cs

35 lines
1.1 KiB
C#
Raw Normal View History

2023-01-18 12:56:24 +00:00
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; }
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);
}
}