upload
This commit is contained in:
103
Data/AppDbContext.cs
Normal file
103
Data/AppDbContext.cs
Normal file
@@ -0,0 +1,103 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user