104 lines
2.6 KiB
C#
104 lines
2.6 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using SimpleWiki.Models;
|
|
|
|
namespace SimpleWiki.Data;
|
|
|
|
public class AppDbContext : DbContext
|
|
{
|
|
public AppDbContext()
|
|
{
|
|
}
|
|
|
|
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
|
|
{
|
|
}
|
|
|
|
public DbSet<User> Users => Set<User>();
|
|
public DbSet<Article> Articles => Set<Article>();
|
|
|
|
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
|
{
|
|
if (!optionsBuilder.IsConfigured)
|
|
{
|
|
optionsBuilder.UseSqlite("Data Source=simplewiki.db");
|
|
}
|
|
}
|
|
|
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
|
{
|
|
modelBuilder.Entity<User>(entity =>
|
|
{
|
|
entity.ToTable("users");
|
|
|
|
entity.HasKey(x => x.Id);
|
|
entity.Property(x => x.Id)
|
|
.ValueGeneratedOnAdd();
|
|
|
|
entity.Property(x => x.FullName)
|
|
.IsRequired()
|
|
.HasMaxLength(100);
|
|
|
|
entity.Property(x => x.Email)
|
|
.IsRequired()
|
|
.HasMaxLength(256);
|
|
|
|
entity.Property(x => x.PasswordHash)
|
|
.IsRequired()
|
|
.HasMaxLength(512);
|
|
|
|
entity.Property(x => x.Role)
|
|
.IsRequired()
|
|
.HasMaxLength(16);
|
|
|
|
entity.Property(x => x.CreatedAt)
|
|
.IsRequired();
|
|
|
|
entity.HasIndex(x => x.Email)
|
|
.IsUnique();
|
|
|
|
entity.HasMany(x => x.Articles)
|
|
.WithOne(x => x.Author)
|
|
.HasForeignKey(x => x.AuthorId)
|
|
.OnDelete(DeleteBehavior.Restrict);
|
|
});
|
|
|
|
modelBuilder.Entity<Article>(entity =>
|
|
{
|
|
entity.ToTable("articles");
|
|
|
|
entity.HasKey(x => x.Id);
|
|
entity.Property(x => x.Id)
|
|
.ValueGeneratedOnAdd();
|
|
|
|
entity.Property(x => x.Title)
|
|
.IsRequired()
|
|
.HasMaxLength(150);
|
|
|
|
entity.Property(x => x.Slug)
|
|
.IsRequired()
|
|
.HasMaxLength(180);
|
|
|
|
entity.Property(x => x.Summary)
|
|
.IsRequired()
|
|
.HasMaxLength(400);
|
|
|
|
entity.Property(x => x.MarkdownContent)
|
|
.IsRequired()
|
|
.HasMaxLength(20000);
|
|
|
|
entity.Property(x => x.CreatedAt)
|
|
.IsRequired();
|
|
|
|
entity.Property(x => x.UpdatedAt)
|
|
.IsRequired(false);
|
|
|
|
entity.HasIndex(x => x.Slug)
|
|
.IsUnique();
|
|
|
|
entity.HasIndex(x => x.Title);
|
|
});
|
|
|
|
base.OnModelCreating(modelBuilder);
|
|
}
|
|
}
|