survey-webapp/SurveyBackend/SurveyBackend.Infrastructure/Data/ApplicationDbContext.cs

34 lines
No EOL
950 B
C#

using Microsoft.EntityFrameworkCore;
using SurveyBackend.Core.Models;
using SurveyLib.Core.Models;
using SurveyLib.Infrastructure.EFCore.Data;
namespace SurveyBackend.Infrastructure.Data;
public class ApplicationDbContext : SurveyDbContext
{
public DbSet<User> Users { get; set; }
public DbSet<Group> Groups { get; set; }
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Survey>()
.HasOne<User>()
.WithMany()
.HasForeignKey(s => s.CreatedBy)
.OnDelete(DeleteBehavior.SetNull);
modelBuilder.Entity<Completion>()
.HasOne<User>()
.WithMany()
.HasForeignKey(c => c.CompletedBy)
.OnDelete(DeleteBehavior.SetNull);
}
}