C
C#2y ago
Eple

❔ DbUpdateConcurrencyException

Getting DbUpdateConcurrencyException on Save
5 Replies
Eple
Eple2y ago
On the following page
public class EditModel : PageModel
{
private readonly ApplicationDbContext _context;

public EditModel(ApplicationDbContext context)
{
_context = context;
}

[BindProperty]
public new ApplicationUser User { get; set; } = default!;

public async Task<IActionResult> OnGetAsync(string? id)
{
if (id == null || _context.Users == null)
{
return NotFound();
}

var user = await _context.Users.FirstOrDefaultAsync(m => m.Id == id);

if (user == null)
{
return NotFound();
}

User = user;
return Page();
}

// To protect from overposting attacks, enable the specific properties you want to bind to.
// For more details, see https://aka.ms/RazorPagesCRUD.
public async Task<IActionResult> OnPostAsync()
{
if (!ModelState.IsValid)
{
return Page();
}

_context.Attach(User).State = EntityState.Modified;

try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!UserExists(User.Id))
{
return NotFound();
}
else
{
throw;
}
}

return RedirectToPage("./Index");
}

private bool UserExists(string id)
{
return (_context.Users?.Any(e => e.Id == id)).GetValueOrDefault();
}
}
public class EditModel : PageModel
{
private readonly ApplicationDbContext _context;

public EditModel(ApplicationDbContext context)
{
_context = context;
}

[BindProperty]
public new ApplicationUser User { get; set; } = default!;

public async Task<IActionResult> OnGetAsync(string? id)
{
if (id == null || _context.Users == null)
{
return NotFound();
}

var user = await _context.Users.FirstOrDefaultAsync(m => m.Id == id);

if (user == null)
{
return NotFound();
}

User = user;
return Page();
}

// To protect from overposting attacks, enable the specific properties you want to bind to.
// For more details, see https://aka.ms/RazorPagesCRUD.
public async Task<IActionResult> OnPostAsync()
{
if (!ModelState.IsValid)
{
return Page();
}

_context.Attach(User).State = EntityState.Modified;

try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!UserExists(User.Id))
{
return NotFound();
}
else
{
throw;
}
}

return RedirectToPage("./Index");
}

private bool UserExists(string id)
{
return (_context.Users?.Any(e => e.Id == id)).GetValueOrDefault();
}
}
Tvde1
Tvde12y ago
You probably don't want to give entities to your front-end and attach them again later, that seems like a dangerous thing to do It will also cause trouble if you want to update either your front-end or your database model but not both at the same time
Eple
Eple2y ago
You are saying I should make an intermediate model and in OnGet get from database and assign to that model and display that model in view?
Tvde1
Tvde12y ago
Usually an intermediate model is used and on post, the entity is fetched and its properties are set using the intermediate object
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.