我尝试使用Entity Framework Code First插入记录。在尝试保存记录时,我不断收到以下错误。
存储更新,插入或删除语句会影响意外的行数(0)。自实体加载后,实体可能已被修改或删除。刷新ObjectStateManager条目。
描述:执行当前Web请求期间发生了未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。
异常详细信息:System.Data.OptimisticConcurrencyException:存储更新,插入或删除语句影响了意外的行数(0)。自实体加载后,实体可能已被修改或删除。刷新ObjectStateManager条目。
型号:
[Table("tblActual_CategoryItem")]
public class Actual_CategoryItem
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int ID { get; set; }
public int? AgencyID { get; set; }
public string UniqueURL { get; set; }
public string Name { get; set; }
public string Price { get; set; }
public string Location { get; set; }
public string LastUpdated { get; set; }
public string Description { get; set; }
public string PhotoURL1 { get; set; }
public string PhotoURL2 { get; set; }
public string AgencyName { get; set; }
public string AgencyUniqueURL { get; set; }
public string AgencyPhotoURL { get; set; }
public string AgentName { get; set; }
public string AgentContactNo { get; set; }
public string AgentEmail { get; set; }
public DateTime? ImportedDateTime { get; set; }
public int? CategoryID { get; set; }
public int? UserID { get; set; }
public string Suburb { get; set; }
public string State { get; set; }
public string PostCode { get; set; }
[ForeignKey("CategoryID")]
public CategoryType Category { get; set; }
[ForeignKey("AgencyID")]
public Actual_AgencyProfile agency { get; set; }
[ForeignKey("UserID")]
public UserProfile userProfile { get; set; }
}
控制器:
[HttpPost]
public ActionResult Create([Bind(Exclude = "ID")]Actual_CategoryItem actual_categoryitem)
{
if (ModelState.IsValid)
{
db.ItemList.Add(actual_categoryitem);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.CategoryID = new SelectList(db.CategoryTypes, "CategoryID", "TypeName", actual_categoryitem.CategoryID);
ViewBag.AgencyID = new SelectList(db.Actual_AgencyProfile, "ID", "UniqueURL", actual_categoryitem.AgencyID);
return View(actual_categoryitem);
}
查看:
@using (Html.BeginForm())
{
@Html.ValidationSummary(false)
@Html.HiddenFor(model => model.ID)
<div class="left-col">
<div class="clearfix"></div>
<br />
<div class="column-a col-md-12 col-sm-12 col-xs-12" style="padding-top: 0px;">
<div>
@Html.ActionLink("Back to List", "Index")
</div>
<div style="height: auto">
<section class="widget">
<h4 class="widget-title">Add New Post</h4>
<div class="kopa-one-two">
<section class="widget">
<div class="entry-item">
<div class="col-md-12"></div>
</div>
<div id="divAgency">
<div class="entry-item">
<div class="col-md-12">Category : </div>
</div>
<div class="entry-item">
<div class="col-md-12">
@Html.DropDownList("CategoryID", (IEnumerable<SelectListItem>)ViewBag.CategoryID, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.CategoryID)
</div>
</div>
<div class="clearfix"></div>
</div>
<div class="entry-item">
<div class="col-md-12">Business Name: </div>
</div>
<div class="entry-item">
<div class="col-md-12">
@Html.TextBoxFor(o => o.Name, new { @class = "form-control" })
</div>
</div>
<div class="clearfix"></div>
<div class="entry-item">
<div class="col-md-12">Sale Price : </div>
</div>
<div class="entry-item">
<div class="col-md-12">
@Html.TextBoxFor(o => o.Price, new { @class = "form-control" })
</div>
</div>
<div class="clearfix"></div>
<div class="entry-item">
<div class="col-md-12">Street Address : </div>
</div>
<div class="entry-item">
<div class="col-md-12">
@Html.TextBoxFor(o => o.Location, new { @class = "form-control" })
</div>
</div>
<div class="clearfix"></div>
<div class="entry-item">
<div class="col-md-12">State: </div>
</div>
<div class="entry-item">
<div class="col-md-12">
@Html.DropDownListFor(o => o.State, new List<SelectListItem>() { ( new SelectListItem() { Text = "New South Wales", Value = "NSW" }),( new SelectListItem() { Text = "Victoria", Value = "VIC" }),
( new SelectListItem() { Text = "Queensland", Value = "QLD" }),( new SelectListItem() { Text = "Western Australia", Value = "WA" })
,( new SelectListItem() { Text = "ACT", Value = "ACT" }),( new SelectListItem() { Text = "South Australia", Value = "SA" })
,( new SelectListItem() { Text = "Tasmania", Value = "TAS" }),( new SelectListItem() { Text = "Northern Territory", Value = "NT" })}
, "Please Select", new { @class = "form-control" })
</div>
</div>
<div class="clearfix"></div>
</section>
</div>
<!--kopa-one-third-->
<div class="kopa-one-two last">
<section class="widget">
<div class="entry-item">
<div class="col-md-12">Suburb: </div>
</div>
<div class="entry-item">
<div class="col-md-12">
@Html.TextBoxFor(o => o.Suburb, new { @class = "form-control" })
</div>
</div>
<div class="clearfix"></div>
<div class="entry-item">
<div class="col-md-12">Post Code: </div>
</div>
<div class="entry-item">
<div class="col-md-12">
@Html.TextBoxFor(o => o.PostCode, new { @class = "form-control" })
</div>
</div>
<div class="clearfix"></div>
<div class="entry-item">
<div class="col-md-12">Contact No: </div>
</div>
<div class="entry-item">
<div class="col-md-12">
@Html.TextBoxFor(o => o.AgentContactNo, new { @class = "form-control" })
</div>
</div>
<div class="clearfix"></div>
<div class="entry-item">
<div class="col-md-12">Contact Email: </div>
</div>
<div class="entry-item">
<div class="col-md-12">
@Html.TextBoxFor(o => o.AgentEmail, new { @class = "form-control" })
</div>
</div>
</section>
</div>
<!--older-posts-->
</section>
<!--widget-->
<div class="clearfix"></div>
<br />
<div class="entry-item">
<div class="col-md-12" style="text-align: right">
<input type="submit" id="submit-comment" value="Submit" class="btn btn-info" />
</div>
</div>
</div>
</div>
<div style="padding-left: 30px">
@Html.ActionLink("Back to List", "Index")
</div>
</div>
<div class="right-col">
.
</div>
}
答案 0 :(得分:0)
您正在使用实体ID上的Bind(Exclude)属性,该属性导致实体ID的值在HttpPost上为零。 由于忘记在.cshtml页面中添加隐藏的ID输入而导致我遇到了这个问题
答案 1 :(得分:0)
当您在数据库表上有SQL触发器时,会发现EF会产生此错误。在触发器结束时添加以下内容解决了问题。然而,这可能不是一个完美的解决方案。
SELECT * FROM deleted UNION ALL
SELECT * FROM inserted;