另一個大問題,在這裏。獲取消息「存儲更新,插入或刪除語句影響了意外的行數(0)」?
我有模式:
public class Lead
{
[System.ComponentModel.DataAnnotations.Column("le_codlead")]
public int LeadID {get; set; }
[System.ComponentModel.DataAnnotations.Column("le_descr1")]
[Required(ErrorMessage="Inserire Nome del Lead")]
[Display(Name="Nominativo Lead")]
public string Name1 {get; set;}
[System.ComponentModel.DataAnnotations.Column("le_descr2")]
[Display(Name = "Nominativo secondario")]
public string Name2 { get; set; }
...
...
...
}
控制器(用於創建方法):
[HttpPost]
public ActionResult Create(Lead lead)
{
if (ModelState.IsValid)
{
db.Leads.Add(lead);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(lead);
}
,這是觀:
@model CRMArcadia.Models.Lead
@{
ViewBag.Title = "Create";
}
<h2>Create</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, "ATTENZIONE! Compilare tutti i campi obbligatori")
<fieldset>
<legend>Lead</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Name1)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name1)
@Html.ValidationMessageFor(model => model.Name1)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Name2)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name2)
@Html.ValidationMessageFor(model => model.Name2)
</div>
...
...
...
<p>
<input type="submit" value="Create" />
</p>
</fieldset> }
但是,當我嘗試插入一個新的Lead會拋出異常,如在Post Title:Entity Framework中:「存儲更新,插入或刪除影響的語句意外的行數(0)。「我在網上讀到它是由ID鍵(LeadID,在這種情況下)沒有更新引起的:他們建議在ID視圖中插入一個hiddenfor屬性,所以我做了:
@Html.HiddenFor(model => model.LeadID)
但是這個在Create按鈕的壓力下沒有任何反應。
有人對此問題有解決方案(或解決方法;))嗎?
謝謝。
您是否在使用代碼優先的方法? –
你的意思是什麼都沒有發生?你進入控制器操作?你傳遞了ModelState.IsValid嗎?你想更新這個模型嗎?或插入新的值? –
什麼是您的EF數據庫上下文?有些東西沒有設置在那裏,你需要調查它。 –