58 lines
1.6 KiB
C#
58 lines
1.6 KiB
C#
using HackerNet.Application;
|
|
using HackerNet.Domain;
|
|
using HackerNet.Infrastructure.Repositories.EntityFramework.EntityTypeConfigurations;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace HackerNet.Infrastructure.Repositories.EntityFramework;
|
|
|
|
public class HackerContext : DbContext
|
|
{
|
|
#pragma warning disable CS8618
|
|
|
|
public DbSet<Link> Links { get; set; }
|
|
public DbSet<Comment> Comments { get; set; }
|
|
|
|
#pragma warning restore CS8618
|
|
|
|
public HackerContext() : base()
|
|
{
|
|
|
|
}
|
|
|
|
public HackerContext(DbContextOptions<HackerContext> options) : base(options)
|
|
{
|
|
|
|
}
|
|
|
|
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
|
{
|
|
if (!optionsBuilder.IsConfigured)
|
|
{
|
|
optionsBuilder.UseSqlite("Data Source=:memory:");
|
|
}
|
|
}
|
|
|
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
|
{
|
|
// var link = modelBuilder.Entity<Link>();
|
|
// link.HasKey(o => o.Id);
|
|
// link.Property(o => o.Url).IsRequired().HasMaxLength(250);
|
|
// link.Property(o => o.Description).IsRequired();
|
|
// link.Property(o => o.CreatedAt).IsRequired();
|
|
|
|
// var comment = modelBuilder.Entity<Comment>();
|
|
// comment.HasKey(o => o.Id);
|
|
// comment.HasOne<Link>()
|
|
// .WithMany()
|
|
// .HasForeignKey(o => o.LinkId)
|
|
// .IsRequired()
|
|
// .OnDelete(DeleteBehavior.Cascade);
|
|
// comment.Property(o => o.Content).IsRequired();
|
|
// comment.Property(o => o.CreatedAt).IsRequired();
|
|
|
|
//modelBuilder.ApplyConfiguration(new LinkEntityTypeConfiguration());
|
|
//modelBuilder.ApplyConfiguration(new CommentEntityTypeConfiguration());
|
|
|
|
modelBuilder.ApplyConfigurationsFromAssembly(typeof(HackerContext).Assembly);
|
|
}
|
|
} |