1
在我的代碼中,更新發生在視圖和控制器中。用於創建和更新的相同控制器和視圖。創建個人資料工作正常但在更新時顯示DbConcurrency異常消息。請幫我找。實體框架更新在更新記錄時顯示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);
}
您可以發佈您的評論作爲顯示其他呃讀者說問題解決了?你可以接受你自己的答案。 – Slauma 2012-04-25 17:16:54