Files
2026-08-05 21:15:15 +03:00

108 lines
4.3 KiB
C#

using Microsoft.EntityFrameworkCore;
using Chat.Domain.Entities;
namespace Chat.Domain;
public class Context : DbContext
{
public DbSet<Message> Messages { get; set; }
public DbSet<Conversation> Conversations { get; set; }
public DbSet<UserPresence> UserPresences { get; set; }
public Context(DbContextOptions<Context> options)
: base(options) { }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
// Configure Message entity
modelBuilder.Entity<Message>(entity =>
{
entity.HasKey(e => e.Id);
entity.Property(e => e.Content).IsRequired().HasMaxLength(5000);
entity.Property(e => e.Status).IsRequired();
entity.Property(e => e.SentAt).IsRequired();
entity.Property(e => e.IsRead).IsRequired();
entity.Property(e => e.IsDeleted).IsRequired();
entity.Property(e => e.IsEdited).IsRequired();
// Indexes for better query performance
entity.HasIndex(e => e.SenderId);
entity.HasIndex(e => e.ReceiverId);
entity.HasIndex(e => e.SentAt);
entity.HasIndex(e => e.Status);
// User foreign key relationships (will be configured when User entity exists)
// entity.HasOne(e => e.Sender)
// .WithMany()
// .HasForeignKey(e => e.SenderId)
// .OnDelete(DeleteBehavior.Restrict);
// entity.HasOne(e => e.Receiver)
// .WithMany()
// .HasForeignKey(e => e.ReceiverId)
// .OnDelete(DeleteBehavior.Restrict);
});
// Configure Conversation entity
modelBuilder.Entity<Conversation>(entity =>
{
entity.HasKey(e => e.Id);
entity.Property(e => e.Participant1Id).IsRequired();
entity.Property(e => e.Participant2Id).IsRequired();
entity.Property(e => e.CreatedAt).IsRequired();
entity.Property(e => e.UnreadCount1).IsRequired();
entity.Property(e => e.UnreadCount2).IsRequired();
// Indexes for better query performance
entity.HasIndex(e => e.Participant1Id);
entity.HasIndex(e => e.Participant2Id);
entity.HasIndex(e => e.CreatedAt);
entity.HasIndex(e => e.LastMessageAt);
entity.HasIndex(e => new { e.Participant1Id, e.Participant2Id });
// Unique constraint to prevent duplicate conversations between the same two users
// (order-agnostic: a conversation between users A and B is the same as between B and A)
// This can be enforced at application level or with a custom constraint
// User foreign key relationships (will be configured when User entity exists)
// entity.HasOne(e => e.Participant1)
// .WithMany()
// .HasForeignKey(e => e.Participant1Id)
// .OnDelete(DeleteBehavior.Restrict);
// entity.HasOne(e => e.Participant2)
// .WithMany()
// .HasForeignKey(e => e.Participant2Id)
// .OnDelete(DeleteBehavior.Restrict);
// Message foreign key relationship (will be configured when Message entity exists)
// entity.HasOne(e => e.LastMessage)
// .WithMany()
// .HasForeignKey(e => e.LastMessageId)
// .OnDelete(DeleteBehavior.SetNull);
});
// Configure UserPresence entity
modelBuilder.Entity<UserPresence>(entity =>
{
entity.HasKey(e => e.UserId);
entity.Property(e => e.Status).IsRequired();
entity.Property(e => e.LastSeenAt).IsRequired();
entity.Property(e => e.ConnectionId).HasMaxLength(255);
// Indexes for better query performance
entity.HasIndex(e => e.Status);
entity.HasIndex(e => e.ConnectionId);
entity.HasIndex(e => e.LastSeenAt);
// User foreign key relationship (will be configured when User entity exists)
// entity.HasOne(e => e.User)
// .WithOne()
// .HasForeignKey<UserPresence>(e => e.UserId)
// .OnDelete(DeleteBehavior.Cascade);
});
}
}