简体   繁体   English

MVC应用程序中的OptimisticConcurrencyException

[英]OptimisticConcurrencyException in MVC application

I'm following the MvcMusicStore tutorial, and all of a sudden I'm having a problem with the editing functionality, which definitely was not going on when I coded that portion. 我正在关注MvcMusicStore教程,突然之间我在编辑功能方面遇到了问题,当我对该部分进行编码时,这肯定没有发生。 Here's the relevant controller... 这是相关的控制器...

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcMusicStore.Models;

namespace MvcMusicStore.Controllers
{
    public class StoreManagerController : Controller
    {
        private MusicStoreEntities db = new MusicStoreEntities();

        //
        // GET: /StoreManager/

        public ViewResult Index()
        {
            var albums = db.Albums.Include(a => a.Genre).Include(a => a.Artist);
            return View(albums.ToList());
        }

        //
        // GET: /StoreManager/Details/5

        public ViewResult Details(int id)
        {
            Album album = db.Albums.Find(id);
            return View(album);
        }

        //
        // GET: /StoreManager/Create

        public ActionResult Create()
        {
            ViewBag.GenreId = new SelectList(db.Genres, "GenreId", "Name");
            ViewBag.ArtistId = new SelectList(db.Artists, "ArtistId", "Name");
            return View();
        }

        //
        // POST: /StoreManager/Create

        [HttpPost]
        public ActionResult Create(Album album)
        {
            if (ModelState.IsValid)
            {
                db.Albums.Add(album);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.GenreId = new SelectList(db.Genres, "GenreId", "Name", album.GenreId);
            ViewBag.ArtistId = new SelectList(db.Artists, "ArtistId", "Name", album.ArtistId);
            return View(album);
        }

        //
        // GET: /StoreManager/Edit/5

        public ActionResult Edit(int id)
        {
            Album album = db.Albums.Find(id);
            ViewBag.GenreId = new SelectList(db.Genres, "GenreId", "Name", album.GenreId);
            ViewBag.ArtistId = new SelectList(db.Artists, "ArtistId", "Name", album.ArtistId);
            return View(album);
        }

        //
        // POST: /StoreManager/Edit/5

        [HttpPost]
        public ActionResult Edit(Album album)
        {
            if (ModelState.IsValid)
            {
                db.Entry(album).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.GenreId = new SelectList(db.Genres, "GenreId", "Name", album.GenreId);
            ViewBag.ArtistId = new SelectList(db.Artists, "ArtistId", "Name", album.ArtistId);
            return View(album);
        }

        //
        // GET: /StoreManager/Delete/5

        public ActionResult Delete(int id)
        {
            Album album = db.Albums.Find(id);
            return View(album);
        }

        //
        // POST: /StoreManager/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Album album = db.Albums.Find(id);
            db.Albums.Remove(album);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}

When the Edit view posts to submit an edit, the ModelState.Valid expression passes, but the AlbumId of the passed album parameter is always zero, though on the GET portion on of the edit methods the AlbumId is there. 当“编辑”视图发布以提交编辑时,将传递ModelState.Valid表达式, 但是传递的专辑参数的AlbumId始终为零,尽管在编辑方法的GET部分上,该AlbumId也存在。 So I'm wondering if maybe this is the reason the db.Entry(album) expression keeps returning 0 rows? 所以我想知道这是否是db.Entry(album)表达式始终返回0行的原因吗? Anyway, that's what's happening, the db.Entry() function keeps returning 0 rows, and then when the context tries to save it throws Exception Details: System.Data.OptimisticConcurrencyException: Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. Refresh ObjectStateManager entries. 无论如何,这就是正在发生的事情,db.Entry()函数不断返回0行,然后当上下文尝试保存它时抛出Exception Details: System.Data.OptimisticConcurrencyException: Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. Refresh ObjectStateManager entries. Exception Details: System.Data.OptimisticConcurrencyException: Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. Refresh ObjectStateManager entries.

This wasn't happening before, and since this happened I've literally copied and pasted this controller and the corresponding edit view from the tutorial download. 以前从未发生过这种情况,并且由于发生了这种情况,因此我从字面上直接复制并粘贴了该控制器和教程下载中的相应编辑视图。 Same error. 同样的错误。 Here's the edit view, but now I'm wondering if maybe the issue isn't somewhere else entirely, though I can't imagine where. 这是编辑视图,但是现在我想知道问题是否还不完全在其他地方,尽管我无法想象在哪里。

@model MvcMusicStore.Models.Album

@{
    ViewBag.Title = "Edit";
}

<h2>Edit</h2>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Album</legend>

        @Html.HiddenFor(model => model.AlbumId)

        <div class="editor-label">
            @Html.LabelFor(model => model.GenreId, "Genre")
        </div>
        <div class="editor-field">
            @Html.DropDownList("GenreId", String.Empty)
            @Html.ValidationMessageFor(model => model.GenreId)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.ArtistId, "Artist")
        </div>
        <div class="editor-field">
            @Html.DropDownList("ArtistId", String.Empty)
            @Html.ValidationMessageFor(model => model.ArtistId)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Title)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Title)
            @Html.ValidationMessageFor(model => model.Title)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Price)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Price)
            @Html.ValidationMessageFor(model => model.Price)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.AlbumArtUrl)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.AlbumArtUrl)
            @Html.ValidationMessageFor(model => model.AlbumArtUrl)
        </div>

        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

The accepted answer here should help out. 此处接受的答案应该会有所帮助。 The most straight forward way around this is to add the id parameter to the Edit() Post method, and then update the Album object, ie 解决此问题的最直接的方法是将id参数添加到Edit()Post方法,然后更新Album对象,即

[HttpPost]
public ActionResult Edit(Album album, int id)
{
    if (ModelState.IsValid)
    {
        album.AlbumId = id;
        db.Entry(album).State = EntityState.Modified;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在MVC应用程序中修复OptimisticConcurrencyException - How to fix OptimisticConcurrencyException in an MVC application ASP.NET MVC OptimisticConcurrencyException - ASP.NET MVC OptimisticConcurrencyException 不抛出OptimisticConcurrencyException:可能的原因:UpdateCommand - OptimisticConcurrencyException is not thrown: Possible reason: UpdateCommand 删除具有父级的ApplicationUser时的OptimisticConcurrencyException - OptimisticConcurrencyException when deleting an ApplicationUser that has a parent OptimisticConcurrencyException在System.Data中不存在 - OptimisticConcurrencyException does not exist in System.Data 在某些情况下,OptimisticConcurrencyException在实体框架中不起作用 - OptimisticConcurrencyException Does Not Work in Entity Framework In Certain Situations ConcurrencyCheck 在第一次更新时抛出 OptimisticConcurrencyException - ConcurrencyCheck throws OptimisticConcurrencyException on first single update 什么NuGet包包含DotNet Core的OptimisticConcurrencyException? - What NuGet package contains OptimisticConcurrencyException for DotNet Core? 为简单的获取/更新方法引发的OptimisticConcurrencyException - OptimisticConcurrencyException thrown for a simple fetch/update method 在MVC应用程序中使用日期 - Using Dates in an MVC application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM