1

在我的代码中,更新发生在视图和控制器中。创建和更新的控制器和视图相同。创建配置文件工作正常。但是在更新它时会显示 DbConcurrency 异常消息。请帮我找。

更新记录时出现异常消息:存储更新、插入或删除语句影响了意外数量的行 (0)。自加载实体后,实体可能已被修改或删除。刷新 ObjectStateManager 条目。

我的更新个人资料页面有(Profile.cshtml)

@model Sitecss.Models.Profile
           @using Microsoft.Web.Helpers;
@{
    ViewBag.Title = "CreateProfile";
    Layout = "~/Views/Shared/_HomeLay.cshtml";
}



@{
    var GT = new SelectList(new[] {
        new {ID ="Team DeathMatch", Name="Team DeathMatch"},
        new {ID ="Search & Destroy", Name="Search & Destroy"},
        new {ID ="Flag Runner", Name="Flag Runner"},
        new {ID ="Domination", Name="Domination"},
        new {ID ="Kill Confirmed", Name="Kill Confirmed"}
    },"ID","Name");
    var Spec = new SelectList(new []{
        new {ID ="Assault", Name="Assault"},
        new {ID ="Tactical", Name="Tactical"},
        new {ID ="Long-Range Eliminations", Name="Long-Range Eliminations"},
        new {ID ="Noob", Name="Noob"}
    }, "ID", "Name");

}


<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("CreateProfile", "Home", FormMethod.Post, new { @encType = "multipart/form-data" }))
{

    <div id="content1">
    @Html.ValidationSummary(true)
<h4>Create a Profile</h4>

    <div>
    @Html.LabelFor(m => m.SteamName)
    </div>
    <div>
    @Html.TextBoxFor(m => m.SteamName, new { @class = "wide" })
    @Html.ValidationMessageFor(m => m.SteamName)
    </div>

    <div>
    @Html.LabelFor(m =>m.UserImg)
    </div>
    <div>

    <input type="file" name="image" class="image" /> <br />
    </div>

    <div>
    @Html.LabelFor(m => m.GameType)
    </div>
    <div>
    @Html.DropDownListFor(m => m.GameType, GT, new { @class = "wide" })
    @Html.ValidationMessageFor(m => m.GameType)
    </div>

    <div>
    @Html.LabelFor(m => m.Specialist)
    </div>
    <div>
    @Html.DropDownListFor(m => m.Specialist, Spec, new { @class = "wide" })
    @Html.ValidationMessageFor(m => m.Specialist)
    </div>

    <div>
    @Html.LabelFor(m => m.FavGun)
    </div>
    <div>
    @Html.TextBoxFor(m => m.FavGun, new { @class = "wide" })
    @Html.ValidationMessageFor(m => m.FavGun)
    </div>
    <p><input type="submit" class="button" value="Ok" /></p>
    </div>
}

而我的控制器功能是

public ActionResult CreateProfile()
        {
            if (db.Profiles.Any(u => u.Username == User.Identity.Name))
            {
                Profile pro = (from usr in db.Profiles where usr.Username == User.Identity.Name select usr).Single();
                olf = pro.UserImg;
                Profile p = db.Profiles.Find(pro.Id);
                ViewBag.ProfilePic = olf;
                return View(p);
            }
            else
            {
                ViewBag.Name = User.Identity.Name;
                return View();
            }
        }


   [HttpPost]
        public ActionResult CreateProfile(Profile m)
        {
            WebImage photo = WebImage.GetImageFromRequest();

            string newFileName = "";
            string thumbs = "";
            if (photo != null)
            {
                string ext = Path.GetExtension(photo.FileName);
                newFileName = User.Identity.Name+ ext;
                thumbs = @"Images/" + newFileName;
                photo.Resize(width: 120, height: 120, preserveAspectRatio: true, preventEnlarge: true);
                photo.Save(@"~/"+thumbs);
                m.UserImg = newFileName;
            }

            if (ModelState.IsValid)
            {    
                if (db.Profiles.Any(u => u.Username == User.Identity.Name))
                {   

                    db.Entry(m).State = System.Data.EntityState.Modified;
                    db.SaveChanges();
                    return RedirectToAction("Index");

                }
                else
                {
                    m.Username = User.Identity.Name;
                    db.Profiles.Add(m);
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
            }
            return View(m);
        }
4

1 回答 1

0

在我的编辑控制器中向更新语句添加断点时,我知道更新时主键 Id = 0 为了解决此问题,我在已修复 DbConcurrency 更新的视图中添加了隐藏字段

于 2012-04-27T12:50:03.743 回答