![](/img/trans.png)
[英]EF 5 - The number of primary key values passed must match the number of primary key values defined on the entity
[英]“The number of primary key values passed must match number of primary key values defined on the entity. Parameter name: keyValues” Entity Framework
因此,我在ASP.NET MVC應用程序中將CRUD操作與實體框架一起使用。 我已經制作了一個登錄表單,每個唯一的用戶都獲得一個UserId,我現在嘗試操作這些CRUD操作,以便用戶可以編輯或刪除它的數據庫記錄。 但是正如您在標題中看到的那樣,我不斷收到錯誤消息。
我認為這與我的主鍵有關,下面是一些代碼:
模型:
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])
);
那么,如何使用戶可以更改數據庫中自己的唯一數據?
請幫忙。
矛盾的說法是
var timesheetentry = _db.TimesheetEntries.Find(id);
正如您正確提到的,它與鍵字段的數量有關。 如果是模型,則必須將三個元素傳遞給Find()
。
所以你必須
Edit()
操作,或者 通常,當您收到此異常時,這意味着您正在請求通過其主鍵(或鍵組)來定位特定實體。
如果確定.Find()方法的參數是正確的主鍵數目,請檢查edmx模型是否顯示正確的主鍵數目。
如果您的edmx模型是正確的,那么錯誤只會在另一個地方存在,那就是數據庫本身。 檢查數據庫,並確保您在實際表中實際設置了任何主鍵。
如果您的數據庫表未指定主鍵字段,則edmx將假定每個字段都是鍵。 因此,請確保您的數據庫和實體模型說的一樣。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.