Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException: Database operation expected to affect 1 row(s) but actually affected 0 row(s). Data may have been modified or deleted since entities were loaded. See http://go.microsoft.com/fwlink/?LinkId=527962 for information on understanding and handling optimistic concurrency exceptions.
But when i remove disable tag from my input element it works correctly!!!
What's the problem & how to solve it ?
Note : The given input field (OrderId) is primary key for my Orders table.
Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException: Database operation expected to affect 1 row(s) but actually affected 0 row(s). Data may have been modified or deleted since entities were loaded. See http://go.microsoft.com/fwlink/?LinkId=527962
for information on understanding and handling optimistic concurrency exceptions.
What the error message means is that you sent an entity to EF to be updated on a database for a table record and the update didn't happen 0 rows were affected. You can't send the EF entity to be updated without the primary-key property for the EF entity
having a valid value.
The ID should have been saved to hidden field pointing back to a viewmodel.
EF query maps an entity to a VM and the VM is sent into the view. On data persistence with EF, the VM populates the EF entity that is sent for add or update.
If you find the post has answered your issue, then please mark post as 'answered'.
but As i told in my first post, it exists in my view, just disabled!
I don't know why when it's disabled, my update fail & when it enabled my update works correctly!
No, that's a clunky way of doing it, and I don't think it's working. You can validate and see what is in the object at the time you're trying to persist it to the database by using Quickwatch. If the ID is jacked, then EF is not going to find the record
to update. Myself, I would never use an input tag in the manner that you're trying to do for a primary-key ID property.
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace PublishingCompany.Models
{
public class ArticleVM
{
public class Article
{
public int ArticleId { get; set; }
public int AuthorId { get; set; }
public string AuthorName { get; set; }
[Required(ErrorMessage = "Title is required")]
[StringLength(50)]
public string Title { get; set; }
public string Body { get; set; }
}
public List<Article> Articles { get; set; } = new List<Article>();
}
}
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Member
35 Points
270 Posts
Facing error 'Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException' when input control disa...
Apr 25, 2020 04:44 PM|hamed_1983|LINK
Hi
my problem is that when i disable my input control as follow :
I'm facing this error during update :
But when i remove disable tag from my input element it works correctly!!!
What's the problem & how to solve it ?
Note : The given input field (OrderId) is primary key for my Orders table.
Thanks in advance
Contributor
4923 Points
4195 Posts
Re: Facing error 'Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException' when input control...
Apr 25, 2020 06:38 PM|DA924|LINK
Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException: Database operation expected to affect 1 row(s) but actually affected 0 row(s). Data may have been modified or deleted since entities were loaded. See http://go.microsoft.com/fwlink/?LinkId=527962 for information on understanding and handling optimistic concurrency exceptions.
What the error message means is that you sent an entity to EF to be updated on a database for a table record and the update didn't happen 0 rows were affected. You can't send the EF entity to be updated without the primary-key property for the EF entity having a valid value.
The ID should have been saved to hidden field pointing back to a viewmodel.
https://www.tutorialsteacher.com/mvc/htmlhelper-hidden-hiddenfor
You should learn how to use a viewmodel.
https://www.dotnettricks.com/learn/mvc/understanding-viewmodel-in-aspnet-mvc
EF query maps an entity to a VM and the VM is sent into the view. On data persistence with EF, the VM populates the EF entity that is sent for add or update.
Member
35 Points
270 Posts
Re: Facing error 'Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException' when input control...
Apr 25, 2020 06:43 PM|hamed_1983|LINK
Thanks for reply.
but As i told in my first post, it exists in my view, just disabled!
I don't know why when it's disabled, my update fail & when it enabled my update works correctly!
Contributor
4923 Points
4195 Posts
Re: Facing error 'Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException' when input control...
Apr 25, 2020 08:45 PM|DA924|LINK
No, that's a clunky way of doing it, and I don't think it's working. You can validate and see what is in the object at the time you're trying to persist it to the database by using Quickwatch. If the ID is jacked, then EF is not going to find the record to update. Myself, I would never use an input tag in the manner that you're trying to do for a primary-key ID property.
https://docs.microsoft.com/en-us/visualstudio/debugger/watch-and-quickwatch-windows?view=vs-2019
Contributor
2690 Points
874 Posts
Re: Facing error 'Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException' when input control...
Apr 28, 2020 06:48 AM|Rena Ni|LINK
Hi hamed_1983,
Browsers do not support disabled property to submit with forms by default.You need to change disabled to readonly:
<input asp-for="OrderId" class="form-control" value="@ViewData["_orderID"]" readonly />
Best Regards,
Rena
All-Star
48490 Points
18068 Posts
Re: Facing error 'Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException' when input control...
Apr 28, 2020 07:21 AM|PatriceSc|LINK
Hi,
The problem is that disabled (and if I remember readonly) form fields are not posted. Note that using F12 it would be easy to stil change this value.
More likely you should check on the server side that the user is allowed to change this line (assuming you are trying to avoid tampering).