2017-09-17 71 views
0

我下面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)。 該聲明已被終止。

和之後的後續文本。

任何想法發生了什麼/如何解決它,爲什麼它不是爲我工作? 我不明白爲什麼本教程的示例不會拋出關於「重複鍵」的錯誤,因爲我的幾乎是相同的。

+2

是ID字段定義爲數據庫標識/自動增量? – James

+0

不,它不是。我只是改變了它。 –

+0

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) ); –

回答

0

我認爲EF試圖連接(加入的話),而不是修改它的實體,這是我猜是因爲你的背景可能是跟蹤,因爲你在得到調用的實體,請嘗試使用發現,而不是第一的,因爲find

The Find method on DbSet uses the primary key value to attempt to find an entity tracked by the context. If the entity is not found in the context then a query will be sent to the database to find the entity there. Null is returned if the entity is not found in the context or in the database.

因此,您在更新它時沒有跟蹤此實體,因爲您正在更新跟蹤的實體本身。 所以在你的GetProject,更換此

model.Projects.Where(x => x.Id == id).First() 

與此

model.Projects.Find(id) 
+0

我現在得到這個錯誤:SqlException:當IDENTITY_INSERT設置爲OFF時,無法在表'Projects'中爲標識列插入顯式值。如何在這種類型的項目中將IDENTITY_INSERT設置爲ON? –

+0

你在更改ID嗎? 你不應該改變id – Munzer

+0

這就是它的一部分 - 本教程要求我添加這行:return id.HasValue? model.Projects.Find(id):new Project(){Id = 0},這意味着我在創建新項目時將標識設置爲0。你可以在上面的視圖中看到它是如何使用的。我會嘗試刪除它。 –