2015-02-23 37 views
-1

因此,我在我的ASP.NET MVC應用程序中使用了實體框架的CRUD操作。我做了一個登錄表單,每個獨特的用戶都得到一個UserId,並試圖操作這些CRUD操作,以便用戶可以編輯或刪除它的數據庫記錄。但是我一直收到錯誤,就像你在標題中看到的一樣。「傳遞的主鍵值的數量必須與實體上定義的主鍵值的數量匹配。參數名稱:keyValues」實體框架

我認爲它與我的主鍵做的,這裏的一些代碼:

型號:

public partial class TimesheetEntry 
    { 
     [Key] 
     [Column(Order = 0)] 
     [DatabaseGenerated(DatabaseGeneratedOption.None)] 
     public int UserId { get; set; } 

     [Key] 
     [Column(Order = 1)] 
     [StringLength(50)] 
     public string ProjectId { get; set; } 

     [Key] 
     [Column(Order = 2, TypeName = "date")] 
     public DateTime EntryDate { get; set; } 

     public decimal HoursWorked { get; set; } 

     public virtual Project Project { get; set; } 

     public virtual User User { get; set; } 
    } 

查看:

@model IEnumerable<Aviato.Models.TimesheetEntry> 

@{ 
    Layout = "~/Views/Shared/_Layout.cshtml"; 
} 

<h2>Index</h2> 

<p> 
    @Html.ActionLink("Create", "Create") 
</p> 
<table class="table"> 
    <tr> 
     <th> 
      @Html.DisplayNameFor(model => model.UserId) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.Project.ProjectName) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.EntryDate) 
     </th> 
     <th> 
      @Html.DisplayNameFor(model => model.HoursWorked) 
     </th> 
     <th></th> 
    </tr> 

    @foreach (var item in Model) { 
     <tr> 
      <td> 
       @Html.DisplayFor(modelItem => item.UserId) 
      </td> 
      <td> 
       @Html.DisplayFor(modelItem => item.Project.ProjectName) 
      </td> 
      <td> 
       @Html.DisplayFor(modelItem => item.EntryDate) 
      </td> 
      <td> 
       @Html.DisplayFor(modelItem => item.HoursWorked) 
      </td> 
      <td> 
       @Html.ActionLink("Edit", "Edit", new { id=item.UserId }) | 
       @Html.ActionLink("Details", "Details", new { id=item.UserId }) | 
       @Html.ActionLink("Delete", "Delete", new { id=item.UserId }) 
      </td> 
     </tr> 
    } 

</table> 
<div> 
    @Html.ActionLink("Index", "Index", "User") 
</div> 

控制器:

public class TimesheetEntryController : Controller 
    { 
    private readonly AviatoModel _db = new AviatoModel(); //Database Model 

    public ActionResult Edit(int? id) 
      { 
       if (id == null) 
       { 
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest); 
       } 

       var timesheetentry = _db.TimesheetEntries.Find(id); 

       if (timesheetentry == null) 
       { 
        return HttpNotFound(); 
       } 

       ViewBag.ProjectId = new SelectList(_db.Projects, "ProjectId", "ProjectName", timesheetentry.ProjectId); 
       ViewBag.UserId = new SelectList(_db.Users, "UserId", "SocialSecurityNumber", timesheetentry.UserId); 

       return View(timesheetentry); 
      } 

      [HttpPost] 
      public ActionResult Edit([Bind(Include="UserId,ProjectId,EntryDate,HoursWorked")] TimesheetEntry timesheetentry) 
      { 
       if (ModelState.IsValid) 
       { 
        _db.Entry(timesheetentry).State = System.Data.Entity.EntityState.Modified; 
        _db.SaveChanges(); 
        return RedirectToAction("Index"); 
       } 

       ViewBag.ProjectId = new SelectList(_db.Projects, "ProjectId", "ProjectName", timesheetentry.ProjectId); 
       ViewBag.UserId = new SelectList(_db.Users, "UserId", "SocialSecurityNumber", timesheetentry.UserId); 

       return View(timesheetentry); 
      } 
} 

數據庫表:

CREATE TABLE [dbo].[TimesheetEntries] (
    [UserId]  INT   NOT NULL, 
    [ProjectId] NVARCHAR (50) NOT NULL, 
    [EntryDate] DATE   NOT NULL, 
    [HoursWorked] DECIMAL (8, 1) CONSTRAINT [DF_TimesheetEntries_HoursWorked] DEFAULT ((0.0)) NOT NULL, 
    CONSTRAINT [PK_TimesheetEntries] PRIMARY KEY CLUSTERED ([UserId] ASC, [ProjectId] ASC, [EntryDate] ASC), 
    CONSTRAINT [FK_TimesheetEntries_Users] FOREIGN KEY ([UserId]) REFERENCES [dbo].[Users] ([UserId]), 
    CONSTRAINT [FK_TimesheetEntries_Projects] FOREIGN KEY ([ProjectId]) REFERENCES [dbo].[Projects] ([ProjectId]) 
); 

那麼,我該如何讓用戶可以在數據庫中更改它自己的唯一數據呢?

請幫忙。

回答

2

有衝突的語句是

var timesheetentry = _db.TimesheetEntries.Find(id); 

正如你提到的正確,它與重點領域的數量有關。如果你的模型你必須傳遞三個元素到Find()

所以,你必須

  1. 要麼所有三個元素傳遞給你的Edit()行動或
  2. 有隻有一列(即separaty關鍵因素 - 在你的方法不可取)的唯一密鑰
+0

我只是通過了所有三個要素,但我仍然有一個錯誤:「參數字典包含參數法「的System.Web無效項非可空類型‘System.DateTime的’的‘entryDate’。 Mvc.ActionResult編輯...」 公衆的ActionResult編輯 { 如果(ID == NULL){ 返回 新HttpStatusCodeResult(HttpStatusCode.BadRequest)(INT ID,字符串專案編號,日期entryDate?); } var timesheetentry = _db.TimesheetEntries.Find(id,projectId,entryDate); – 2015-02-23 10:04:21

+0

這聽起來像是從html代碼中傳遞日期的問題。你可以在調用Find()之前調試日期的值嗎? – 2015-02-23 10:05:54

+0

我試圖把一個斷點,它沒有達到它... – 2015-02-23 11:49:35

1

通常,當你得到這個異常時,這意味着你正在請求通過它的主鍵(或鍵組)來定位一個特定的實體。

如果您確定.Find()方法的參數是正確的主鍵數量,請檢查您的edmx模型是否顯示正確的主鍵數量。

如果您的edmx模型是正確的,那麼只有一個其他地方會出現錯誤,即您的數據庫本身。檢查你的數據庫,並確保你真的在實際的表中設置了任何主鍵。

If your database table doesn't specify a primary key field, your edmx will assume each field is a key. So ensure your database and your entity model as saying the same thing.