locked
Facing error 'Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException' when input control disabled! RRS feed

  • Question

  • User-79977429 posted

    Hi

    my problem is that when i disable my input control as follow :

    <div class="form-group">
                    <label asp-for="OrderId" class="control-label"></label>
                    <input asp-for="OrderId" class="form-control" value="@ViewData["_orderID"]" disabled="disabled" />
                    <span asp-validation-for="OrderId" class="text-danger"></span>
                </div>

    I'm facing this error during update :

    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.

    Thanks in advance

    Saturday, April 25, 2020 4:44 PM

Answers

  • User711641945 posted

    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

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 28, 2020 6:48 AM

All replies

  • User1120430333 posted

    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.

    Saturday, April 25, 2020 6:38 PM
  • User-79977429 posted

    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!

    Saturday, April 25, 2020 6:43 PM
  • User1120430333 posted

    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!

    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

    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>();
        }
    }
    @model ArticleVM.Article
    
    <!DOCTYPE html>
    
    <style type="text/css">
        .editor-field > label {
            float: left;
            width: 150px;
        }
    
        txtbox {
            font-family: Arial, Helvetica, sans-serif;
            font-size: 12px;
            background: white;
            color: black;
            cursor: text;
            border-bottom: 1px solid #104A7B;
            border-right: 1px solid #104A7B;
            border-left: 1px solid #104A7B;
            border-top: 1px solid #104A7B;
            padding-top: 10px;
        }
    
        textarea {
            width: 500px;
            height: 75px;
        }
    </style>
    
    <html>
    <head>
        <title>Edit</title>
    </head>
    
    <body>
    <h1>Article</h1>
    
    @using (Html.BeginForm())
    {
        @Html.ValidationSummary(false, "", new { @class = "text-danger" })
    
        <fieldset>
    
            <legend>Edit</legend>
    
            @Html.HiddenFor(model => model.ArticleId)
            @Html.HiddenFor(model => model.AuthorId)
            <div class="editor-field">
                @Html.Label("Title:")
                @Html.TextBoxFor(model => model.Title, new { @Cssclass = "txtbox" })
                @Html.ValidationMessageFor(model => model.Title, "", new { @class = "text-danger" })
            </div>
            <br />
            <div class="editor-field">
                @Html.Label("Body:")
                @Html.TextAreaFor(model => model.Body)
            </div>
            <br />
            <br />
            <p>
                <input type="submit" name="submit" value="Save" />
                <input type="submit" name="submit" value="Cancel" />
            </p>
    
        </fieldset>
    }
    
    </body>
    </html>
    
    
    
    
    
    

    Saturday, April 25, 2020 8:45 PM
  • User711641945 posted

    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

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 28, 2020 6:48 AM
  • User753101303 posted

    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).

    Tuesday, April 28, 2020 7:21 AM