Ich versuche, einen Datensatz mit Entity Framework Code zuerst einfügen. Beim Versuch, den Datensatz zu speichern, erhalte ich den folgenden Fehler.Insert-Anweisung betroffen eine unerwartete Anzahl von Zeilen (0)
Die Aktualisierung, Einfügung oder Löschung einer Anweisung zum Speichern von Dateien hat eine unerwartete Anzahl von Zeilen (0) betroffen. Entitäten wurden möglicherweise geändert oder gelöscht, seit Entitäten geladen wurden. Aktualisieren Sie die ObjectStateManager-Einträge.
Beschreibung: Bei der Ausführung der aktuellen Webanforderung ist eine nicht behandelte Ausnahme aufgetreten. Bitte überprüfen Sie die Stack-Trace für weitere Informationen über den Fehler und wo es aus dem Code stammt.
Ausnahmedetails: System.Data.OptimisticConcurrencyException: Update speichern, einfügen oder löschen Anweisung betroffen eine unerwartete Anzahl von Zeilen (0). Entitäten wurden möglicherweise geändert oder gelöscht, seit Entitäten geladen wurden. Aktualisieren Sie die ObjectStateManager-Einträge.
Modell:
[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; }
}
Controller:
[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);
}
Ausblick:
@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>
}
OP erstellt eine neue Entität. Die ID ist nicht erforderlich (ein automatisch inkrementiertes Feld, dessen Wert von der Datenbank generiert wird) –