因此,我在我的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])
);
那麼,我該如何讓用戶可以在數據庫中更改它自己的唯一數據呢?
請幫忙。
我只是通過了所有三個要素,但我仍然有一個錯誤:「參數字典包含參數法「的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
這聽起來像是從html代碼中傳遞日期的問題。你可以在調用Find()之前調試日期的值嗎? – 2015-02-23 10:05:54
我試圖把一個斷點,它沒有達到它... – 2015-02-23 11:49:35