C#C
C#3y ago
xdd

✅ EF Core delete error

hi, i tried to fix it but nothing works,
so i have dbcontext
public interface IApplicationDbContext
{
    DbSet<Chat> Chats { get; set; }
    DbSet<Message> Messages { get; set; }
    DbSet<Connection> LiveConnections { get; set; }
    int SaveChanges();
    Task<int> SaveChangesAsync(CancellationToken cancellationToken = default);
}

public class ApplicationDbContext : DbContext, IApplicationDbContext
{
    public DbSet<Chat> Chats { get; set; } = null!;
    public DbSet<Message> Messages { get; set; } = null!;
    public DbSet<Connection> LiveConnections { get; set; } = null!;

    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {
        Database.EnsureCreated();
    }

    public override int SaveChanges()
    {
        return base.SaveChanges();
    }

    public override async Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
    {
        return await base.SaveChangesAsync(cancellationToken);
    }
}


and
public class Chat
{
    [Key]
    public int Id { get; set; }
    public int? AuthorId { get; set; } = null!;
    public string Title { get; set; } = null!;
    public List<Connection>? LiveConnections { get; set; }
    public List<Message>? Messages { get; set; }
}


public class Connection
{
    [Key]
    public int? Id { get; set; }
    public string ConnectionId { get; set; } = "Undefined";
    public int? ChatId { get; set; }
    public int? UserId { get; set; }
}

public class Message
{
    [Key]
    public int? Id { get; set; }
    public int? ChatId { get; set; } = null!;
    public int? SenderId { get; set; } = null!;
    public DateTime? Date { get; set; } = DateTime.Now;
    public string? Content { get; set; }
}

so, error only happens when chat have Include (connections / messages). how to make on delete chat don't care about this things?
Was this page helpful?