In my controller named Identities, I have some actions that update new records to the database, for instance, this is edit action:
[HttpPost, ActionName("Edit")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> EditPost(string? id, string[] selectedCourses)
{
if (id == null)
{
return NotFound();
}
var currentUser = await _db.AppUsers
.FirstOrDefaultAsync(u => u.Id == id);
if (currentUser == null)
{
return NotFound();
}
if (currentUser.UserType == "Student")
{
var studentToEdit = await _db.AppUsers
.Include(e => e.Enrollments)
.ThenInclude(c => c.Course)
.AsNoTracking()
.FirstOrDefaultAsync(u => u.Id == id);
if (await TryUpdateModelAsync(studentToEdit, "",
u => u.UserName, u => u.FirstName, u => u.LastName, u => u.Email,
u => u.PhoneNumber, u => u.UserType, u => u.DepartmentID))
{
try
{
// this is for debugging purpose
Console.WriteLine(studentToEdit);
// save changes to db
await _db.SaveChangesAsync();
}
catch (DbUpdateException)
{
// Log the error (uncomment ex variable name and write a log.)
ModelState.AddModelError("", "Unable to save changes. " +
"Try again, and if the problem persists, " +
"see your system administrator.");
}
return RedirectToAction(nameof(Index));
}
}
else
{
var lecturerToEdit = await _db.AppUsers
.Include(ca => ca.CourseAssignments)
.ThenInclude(c => c.Course)
.AsNoTracking()
.FirstOrDefaultAsync(u => u.Id == id);
if (await TryUpdateModelAsync(lecturerToEdit, "",
u => u.UserName, u => u.FirstName, u => u.LastName, u => u.Email,
u => u.PhoneNumber, u => u.UserType, u => u.DepartmentID))
{
try
{
Console.WriteLine(lecturerToEdit);
await _db.SaveChangesAsync();
}
catch (DbUpdateException)
{
//Log the error (uncomment ex variable name and write a log.)
ModelState.AddModelError("", "Unable to save changes. " +
"Try again, and if the problem persists, " +
"see your system administrator.");
}
return RedirectToAction(nameof(Index));
}
}
return View(currentUser);
}
And this is the View:
<div class="container">
<form method="post" asp-controller="Identities" asp-action="Edit">
<div class="row px-2 border p-4 text-center">
<div class="text-danger" asp-validation-summary=ModelOnly></div>
<div class="col-8">
<div class="form-group row">
<div class="col-4">
<label asp-for="UserName"></label>
</div>
<div class="col-8">
<input asp-for="UserName" class="form-control">
<span asp-validation-for="UserName" class="text-danger"></span>
</div>
</div>
<div class="form-group row">
<div class="col-4">
<label asp-for="FirstName"></label>
</div>
<div class="col-8">
<input asp-for="FirstName" class="form-control">
<span asp-validation-for="FirstName" class="text-danger"></span>
</div>
</div>
<div class="form-group row">
<div class="col-4">
<label asp-for="LastName"></label>
</div>
<div class="col-8">
<input asp-for="LastName" class="form-control">
<span asp-validation-for="LastName" class="text-danger"></span>
</div>
</div>
<div class="form-group row">
<div class="col-4">
<label asp-for="Email"></label>
</div>
<div class="col-8">
<input asp-for="Email" class="form-control">
<span asp-validation-for="Email" class="text-danger"></span>
</div>
</div>
<div class="form-group row">
<div class="col-4">
<label asp-for="PhoneNumber"></label>
</div>
<div class="col-8">
<input asp-for="PhoneNumber" class="form-control">
<span asp-validation-for="PhoneNumber" class="text-danger"></span>
</div>
</div>
<div class="form-group row">
<div class="col-4">
<label asp-for="UserType"></label>
</div>
<div class="col-8">
<input asp-for="UserType" class="form-control">
<span asp-validation-for="UserType" class="text-danger"></span>
</div>
</div>
<div class="form-group row">
<div class="col-4">
<label asp-for="Department" class="control-label">Department</label>
</div>
<div class="col-8">
<select class="form-control" asp-for="DepartmentID" asp-items="ViewBag.DepartmentList">
<option value="">-- Select Department --</option>
</select>
</div>
</div>
<div class="form-group row">
<div class="col-8 offset-4">
<div class="row">
<div class="col">
<button type="submit" class="btn btn-primary form-control">Update</button>
</div>
<div class="col">
<a asp-action="Index" class="btn btn-success form-control">Back To List</a>
</div>
</div>
</div>
</div>
</div>
</form>
</div>
My problem: When I tried to update new records and save changes to the database, new records were not updated.
My trials: I've debugged and set breakpoints to track the new records that I inserted in every input field is ok or not. Surprisingly everything was ok. In either Console.Writeline(studentToEdit) or Console.Writeline(lecturerToEdit) includes new record data as I expected. But in the final step when I tried saveChangesAsync, that new records were not saved to the database at all.
AsNoTracking(). You are telling EF Core to not track any changes made to the entity. So nothing will update when saving to the DB.AsNoTracking()inedit postand it worked!