C
C#5d ago
/Mr.Ares77

How to update mysql server data?

Hi, I'm building todo list project server. I wanna update database but have got 500 error.
public async Task<bool> UpdateTodo(Guid userId, Guid todoId, TodoEntity todo)
{
var existingTodo = await _dbContext.todos
.Include(t => t.TodoItems)
.Where(t => t.DeletedAt == null && t.Id == todoId && t.UserId == userId)
.FirstOrDefaultAsync();

if (existingTodo != null)
{
existingTodo.UpdatedAt = DateTime.UtcNow;
existingTodo.DueDate = todo.DueDate;
existingTodo.Title = todo.Title;
existingTodo.TodoItems.Clear();
existingTodo.TodoItems = todo.TodoItems.Select(item => new TodoItemEntity
{
ItemTitle = item.ItemTitle,
Status = item.Status,
Deleted = item.Deleted
}).ToList();

_dbContext.todos.Update(existingTodo);

await _dbContext.SaveChangesAsync();

return true;
}

return false;

}
public async Task<bool> UpdateTodo(Guid userId, Guid todoId, TodoEntity todo)
{
var existingTodo = await _dbContext.todos
.Include(t => t.TodoItems)
.Where(t => t.DeletedAt == null && t.Id == todoId && t.UserId == userId)
.FirstOrDefaultAsync();

if (existingTodo != null)
{
existingTodo.UpdatedAt = DateTime.UtcNow;
existingTodo.DueDate = todo.DueDate;
existingTodo.Title = todo.Title;
existingTodo.TodoItems.Clear();
existingTodo.TodoItems = todo.TodoItems.Select(item => new TodoItemEntity
{
ItemTitle = item.ItemTitle,
Status = item.Status,
Deleted = item.Deleted
}).ToList();

_dbContext.todos.Update(existingTodo);

await _dbContext.SaveChangesAsync();

return true;
}

return false;

}
[HttpPut("{id}")]
public async Task<IActionResult> UpdateDate(Guid id, [FromBody] TodoRequestDto request)
{
var userIdClaim = User.FindFirst(ClaimTypes.NameIdentifier)?.Value;

if (string.IsNullOrEmpty(userIdClaim))
{
return Unauthorized("User not authenticated.");
}

var userId = Guid.Parse(userIdClaim);

var todo = new TodoEntity
{
DueDate = request.DueDate,
Title = request.Title,
TodoItems = request.TodoItems.Select(item => new TodoItemEntity
{
ItemTitle = item.ItemTitle,
Status = item.Status,
Deleted = item.Deleted,
}).ToList(),
};

var result = await _todoService.UpdateTodo(userId, id, todo);

return Ok(new { Success = result });
}
[HttpPut("{id}")]
public async Task<IActionResult> UpdateDate(Guid id, [FromBody] TodoRequestDto request)
{
var userIdClaim = User.FindFirst(ClaimTypes.NameIdentifier)?.Value;

if (string.IsNullOrEmpty(userIdClaim))
{
return Unauthorized("User not authenticated.");
}

var userId = Guid.Parse(userIdClaim);

var todo = new TodoEntity
{
DueDate = request.DueDate,
Title = request.Title,
TodoItems = request.TodoItems.Select(item => new TodoItemEntity
{
ItemTitle = item.ItemTitle,
Status = item.Status,
Deleted = item.Deleted,
}).ToList(),
};

var result = await _todoService.UpdateTodo(userId, id, todo);

return Ok(new { Success = result });
}
30 Replies
Angius
Angius5d ago
What's the actual error?
/Mr.Ares77
/Mr.Ares77OP5d ago
Here is error.
No description
Angius
Angius5d ago
There's no need for that _dbContext.todos.Update(existingTodo);, and your controller doesn't use async methods where it should... but other than that everything seems fine Place a breakpoint and step through the code, see where exactly it breaks
/Mr.Ares77
/Mr.Ares77OP5d ago
Im beginner of C#. This is my first server.😅 So can you review my code?
Angius
Angius5d ago
On second look... it seems fine Could be simplified a tad, I guess
public async Task<bool> UpdateTodo(Guid userId, Guid todoId, TodoEntity todo)
{
var existingTodo = await _dbContext.todos
.Include(t => t.TodoItems)
.Where(t => t.DeletedAt == null && t.Id == todoId && t.UserId == userId)
.FirstOrDefaultAsync();

if (existingTodo != null)
{
existingTodo.UpdatedAt = DateTime.UtcNow;
existingTodo.DueDate = todo.DueDate;
existingTodo.Title = todo.Title;
- existingTodo.TodoItems.Clear();
existingTodo.TodoItems = todo.TodoItems.Select(item => new TodoItemEntity
{
ItemTitle = item.ItemTitle,
Status = item.Status,
Deleted = item.Deleted
}).ToList();

- _dbContext.todos.Update(existingTodo);

await _dbContext.SaveChangesAsync();

return true;
}

return false;

}
public async Task<bool> UpdateTodo(Guid userId, Guid todoId, TodoEntity todo)
{
var existingTodo = await _dbContext.todos
.Include(t => t.TodoItems)
.Where(t => t.DeletedAt == null && t.Id == todoId && t.UserId == userId)
.FirstOrDefaultAsync();

if (existingTodo != null)
{
existingTodo.UpdatedAt = DateTime.UtcNow;
existingTodo.DueDate = todo.DueDate;
existingTodo.Title = todo.Title;
- existingTodo.TodoItems.Clear();
existingTodo.TodoItems = todo.TodoItems.Select(item => new TodoItemEntity
{
ItemTitle = item.ItemTitle,
Status = item.Status,
Deleted = item.Deleted
}).ToList();

- _dbContext.todos.Update(existingTodo);

await _dbContext.SaveChangesAsync();

return true;
}

return false;

}
For debugging tutorial, check $debug
MODiX
MODiX5d ago
Tutorial: Debug C# code and inspect data - Visual Studio (Windows)
Learn features of the Visual Studio debugger and how to start the debugger, step through code, and inspect data in a C# application.
Angius
Angius5d ago
It will teach you how to set breakpoints, step through code, etc
/Mr.Ares77
/Mr.Ares77OP5d ago
Thanks your help.
Angius
Angius5d ago
Also, show your TodoEntity I assume that's the database model The error you got could pop up when it doesn't have a primary key set, IIRC Config for that model too, if it's not in the attributes
/Mr.Ares77
/Mr.Ares77OP5d ago
@Angius Sorry for the inconvenience. But I have got also Internal Server Error. If you don't mind, could you take a look at this code?
Angius
Angius5d ago
Yes, that's what error 500 is
/Mr.Ares77
/Mr.Ares77OP5d ago
GitHub
GitHub - SecretariatV/testing
Contribute to SecretariatV/testing development by creating an account on GitHub.
/Mr.Ares77
/Mr.Ares77OP5d ago
Sorry and thanks your help.
Angius
Angius5d ago
Huh, everything seems fine
/Mr.Ares77
/Mr.Ares77OP5d ago
oh, 😭 Thanks, 👍 😭
Adam Vincent
Adam Vincent5d ago
dbcontext has the relationship between TodoEntity and TodoItemEntity commented out, and that relationship is backwards, no? (it's been a while since I entity framework'd)
// modelBuilder.Entity<TodoItemEntity>()
// .HasOne(ti => ti.Todo)
// .WithMany()
// .HasForeignKey(ti => ti.TodoId)
// .OnDelete(DeleteBehavior.SetNull);
// }
// modelBuilder.Entity<TodoItemEntity>()
// .HasOne(ti => ti.Todo)
// .WithMany()
// .HasForeignKey(ti => ti.TodoId)
// .OnDelete(DeleteBehavior.SetNull);
// }
shouldnt it be .Entity<TodoItemEntity>.WithMany(TodoItemEntity)?
/Mr.Ares77
/Mr.Ares77OP5d ago
I dont know how to use this code. its from chatgpt.😅
Adam Vincent
Adam Vincent5d ago
🤔
/Mr.Ares77
/Mr.Ares77OP5d ago
😢 I had built todo list server using Express and now migrate ASP.NET.
Adam Vincent
Adam Vincent5d ago
you can paste the code back in chatgpt and tell it to explain it to you
/Mr.Ares77
/Mr.Ares77OP5d ago
chatgpt is 💩 can you review my code?
Adam Vincent
Adam Vincent5d ago
I did, and this is what you need to learn to fix it. You have to define the relationship between TodoEntity and TodoItemEntity https://learn.microsoft.com/en-us/ef/core/modeling/relationships
Introduction to relationships - EF Core
How to configure relationships between entity types when using Entity Framework Core
/Mr.Ares77
/Mr.Ares77OP5d ago
Thanks
Angius
Angius5d ago
You don't have to. EF works by convention Having a List<Bar> Bars property in Foo, and Foo Foo property in Bar will create many-to-many for example
Adam Vincent
Adam Vincent5d ago
but that's IF you have the navigation props in the entity types, which he doesn't.
Angius
Angius5d ago
Ah, then yeah Didn't notice the missing props
Adam Vincent
Adam Vincent5d ago
neither did ChatGPT hehe.
Angius
Angius5d ago
LLMs being LLMs
Adam Vincent
Adam Vincent5d ago
this is why I'm not concerned about losing my job to AI.
glhays
glhays4d ago
Assuming the props were not altered post.

Did you find this page helpful?