C
C#ā€¢14mo ago
wwww

āœ… 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 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);
}
}
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 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 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; }
}
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?
9 Replies
jcotton42
jcotton42ā€¢14mo ago
what error? and show the code that's causing the error
wwww
wwwwOPā€¢14mo ago
1 sec, windows update
wwww
wwwwOPā€¢14mo ago
so, error happens when i call this action
[HttpDelete]
public async Task<IActionResult> DeleteChat([FromBody] DeleteChatDTO helper)
{
Chat? chat = await _db.Chats.FirstOrDefaultAsync(chat => chat.Id == helper.ChatId);

if (chat == null)
return NotFound("Chat with this Id not found");

if (chat.AuthorId != helper.SenderId)
return BadRequest("this user doesn't have permissions");

_db.Chats.Remove(chat);
await _db.SaveChangesAsync();

return Ok("Chat successfully deleted");
}
[HttpDelete]
public async Task<IActionResult> DeleteChat([FromBody] DeleteChatDTO helper)
{
Chat? chat = await _db.Chats.FirstOrDefaultAsync(chat => chat.Id == helper.ChatId);

if (chat == null)
return NotFound("Chat with this Id not found");

if (chat.AuthorId != helper.SenderId)
return BadRequest("this user doesn't have permissions");

_db.Chats.Remove(chat);
await _db.SaveChangesAsync();

return Ok("Chat successfully deleted");
}
No description
jcotton42
jcotton42ā€¢14mo ago
how is your OnDelete set up? LiveConnections.ChatId would be referring to a Chat that no longer exists so you either need to manually remove that connection yourself, or set up Cascade or SetNull as the deletion action
wwww
wwwwOPā€¢14mo ago
i gues OnDelete don't exists in my code, atleast i don't wrote it so, when i was googling problem, they sain i need to set up cascade delete but I haven't figured it out how šŸ˜¦
jcotton42
jcotton42ā€¢14mo ago
also, that EnsureCreated in your db context constructor is gonna bite you EnsureCreated is not compatible with migraitons
wwww
wwwwOPā€¢14mo ago
so, i just need to figure out how to cascade delete? for that thing work
jcotton42
jcotton42ā€¢14mo ago
yeah
wwww
wwwwOPā€¢14mo ago
ok thanks a lot
Want results from more Discord servers?
Add your server