using DigitalData.MessagingService.Domain.Entities;
using Microsoft.EntityFrameworkCore;
namespace DigitalData.MessagingService.Infrastructure.Persistence;
///
/// Entity Framework Core DbContext for MessagingService.
///
public class MessagingServiceDbContext(DbContextOptions options) : DbContext(options)
{
public DbSet EmailAccounts => Set();
public DbSet ReceivedEmails => Set();
public DbSet EmailAttachments => Set();
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity(entity =>
{
entity.Property(e => e.Username)
.UseCollation("SQL_Latin1_General_CP1_CI_AS");
});
modelBuilder.Entity(entity =>
{
entity.HasMany(e => e.Attachments)
.WithOne(a => a.Email)
.HasForeignKey(a => a.EmailId)
.OnDelete(DeleteBehavior.Cascade);
entity.HasOne(e => e.Account)
.WithMany()
.HasForeignKey(e => e.AccountId)
.OnDelete(DeleteBehavior.Restrict);
});
}
}