我下面this教程創建自己的自定義「博客」的功能,但不是創建博客文章,它會創建「工程」與創建,短標題,日期描述,長描述和團隊領導。更新數據庫返回主鍵錯誤ASP.NET MVC 5
下面的代碼應該是如果URL通過INT它的主鍵相匹配,以更改項目的信息,如果沒有,創建具有上述所有參數的一個新項目。
然而,當它到達model.SaveChanges(如下所示),它引發錯誤。 我知道我在MVC 5中工作,而教程是在MVC 3中,但到目前爲止一切都翻譯得相當好。
這裏是我的控制器:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SouthMeckNTHS.Models;
using SouthMeckNTHS.Extensions;
using System.Text;
namespace SouthMeckNTHS.Controllers
{
public class ProjectsController : Controller
{
private ProjectsModel model = new ProjectsModel();
// GET: Projects
public ActionResult Index()
{
return RedirectToAction("My");
}
[Authorize]
public ActionResult My()
{
return View();
}
[Authorize]
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Update(int? id, string title, string shortDescription, string longDescription, DateTime dateTime, string technologies)
{
//REMEMBER TO ADD PROJECT LEADER FUNCTIONALITY!!
if (!User.IsInRole("ChapterAdvisor"))
{
RedirectToAction("Index");
}
Project project = GetProject(id);
project.Title = title;
project.ShortDescription = shortDescription;
project.LongDescription = longDescription;
project.TimeCreated = dateTime;
project.ProjectLeader = User.Identity.GetFirstName() + " " + User.Identity.GetLastName();
project.Technologies.Clear();
technologies = technologies ?? string.Empty;
string[] technologyNames = technologies.Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries);
foreach (string technologyName in technologyNames)
{
project.Technologies.Add(GetTechnology(technologyName));
}
if (!id.HasValue)
{
model.Projects.Add(project);
}
try
{
model.SaveChanges();
}
catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)
{
Exception raise = dbEx;
foreach (var validationErrors in dbEx.EntityValidationErrors)
{
foreach (var validationError in validationErrors.ValidationErrors)
{
string message = string.Format("{0}:{1}",
validationErrors.Entry.Entity.ToString(),
validationError.ErrorMessage);
// raise a new exception nesting
// the current instance as InnerException
raise = new InvalidOperationException(message, raise);
}
}
throw raise;
}
return RedirectToAction("Details", new { id = project.Id });
}
[ValidateInput(false)]
public ActionResult Edit(int? id)
{
Project project = GetProject(id);
StringBuilder technologyList = new StringBuilder();
foreach (Technology technology in project.Technologies)
{
technologyList.AppendFormat("{0} ", technology.Name);
}
ViewBag.Technologies = technologyList.ToString();
return View(project);
}
private Technology GetTechnology(string technologyName)
{
return model.Technologies.Where(x => x.Name == technologyName).FirstOrDefault() ?? new Technology() { Name = technologyName };
}
private Project GetProject(int? id)
{
return id.HasValue ? model.Projects.Where(x => x.Id == id).First() : new Project() { Id = 0 };
}
[Authorize]
public ActionResult Join()
{
return View();
}
}
}
這裏是查看:
@model SouthMeckNTHS.Models.Project
@using SouthMeckNTHS.Extensions;
@{
ViewBag.Title = "Edit";
}
<br />
<br />
<br />
<br />
<br /><br />
<form action="@Href("~/Projects/Update")" method="post" id="postForm">
@Html.AntiForgeryToken()
@if (Model.Id != 0)
{
<input type="hidden" name="id" value="@Model.Id" />
}
@{ DateTime dateTime = Model.TimeCreated.Year > 2000 ? Model.TimeCreated : DateTime.Now; }
<input type="text" name="dateTime" value="@dateTime" /> Date<br />
<input type="text" name="title" value="@Model.Title" /> Project Name<br />
@Html.DropDownListFor(m => m.Technologies, new SelectList(new List<Object> { new { value = "Animation", text = "Animation" }, new { value = "Robotics", text = "Robotics" }, new { value = "Architecture", text = "Architecture" }, new { value = "CAD", text = "CAD" }, new { value = "Websites", text = "Websites" }, new { value = "Games", text = "Games" }, new { value = "Biotechnology", text = "Biotechnology" }, new { value = "Club", text = "Club" }, new { value = "Other", text = "Other" } }, "value", "text"), new { @style = "border: 1px solid #e8e8e8;padding: 0.5em 1.07em 0.5em;background: #f5f5f5;font-size: 0.875rem;border-radius: 5px;width: 100%;line-height: 1.43;min-height: 3.5em;" })
<textarea name="shortDescription" rows="5" cols="80">@Model.ShortDescription</textarea><br />
<textarea name="longDescription" rows="10" cols="80">@Model.LongDescription</textarea><br />
<input type="submit" name="submit" value="Save Changes" />
</form>
**,這是錯誤我得到: Click Here to View the image
^上面的圖片是錯誤的閱讀:
違反PRIMARY KEY約束 'PK__Projects__3214EC0766FE6300' 的。不能在對象'dbo.Projects'中插入重複鍵。重複的鍵值是(0)。 該聲明已被終止。
和之後的後續文本。
任何想法發生了什麼/如何解決它,爲什麼它不是爲我工作? 我不明白爲什麼本教程的示例不會拋出關於「重複鍵」的錯誤,因爲我的幾乎是相同的。
是ID字段定義爲數據庫標識/自動增量? – James
不,它不是。我只是改變了它。 –
CREATE TABLE [DBO]。[項目]( [ID] INT NOT NULL IDENTITY, [標題] NVARCHAR(50)NOT NULL, [TimeCreated] DATETIME NOT NULL, [SHORTDESCRIPTION] NVARCHAR(1000)NOT NULL, [LongDescription] NVARCHAR(MAX)NOT NULL, [ProjectLeader] NVARCHAR(1000)NOT NULL, PRIMARY KEY CLUSTERED([Id] ASC) ); –