2015-03-13 35 views
-1

所以即時通訊設法啓用項目的下拉列表啓用取決於如果用戶有一個項目分配給它。不過,我不斷收到錯誤,你的標題看...即時通訊使用ASP.NET MVC 5實體框架傳遞的主鍵值數量必須與實體上定義的主鍵值數量匹配。 ASP.NET MVC實體框架

下面是一些代碼:

控制器:

namespace ReportSystem.Controllers 
{ 
    [AuthenticateRoles] 
    public class TimeReportsController : Controller 
    { 
     private readonly ReportsDatabaseEntities _db = new ReportsDatabaseEntities(); //Database Connection 

     [HttpPost] 
     public ActionResult Login(LoginModel model) 
     { 
      var user = _db.Consultants.FirstOrDefault(i => i.Name == model.Name); 

      if (user != null) 
      { 
       if (user.Password == model.Password) 
       { 
        Session["Name"] = user.Name; 
        Session["RoleID"] = user.RoleID; 
        Session["Id"] = user.Id; 

        return RedirectToAction("Index", "Home"); 
       } 
      } 

      return View("Login"); 
     } 

     public ActionResult Index() 
     { 
      var consultantId = (int)Session["Id"]; 
      var timereports = _db.TimeReports.Where(c => c.ConsultantID == consultantId).ToList(); 

      return View(timereports); 
     } 

     public ActionResult Create() 
     { 
      var projectMembers = _db.ProjectMembers.Find(); 
      var projectId = _db.ProjectMembers.First(); 

      if (projectMembers.ConsultantID == (int)Session["Id"]) 
      { 
       if (projectId == null) 
       { 
        ViewBag.ProjectID = null; 
       } 
      } 

      ViewBag.ProjectID = new SelectList(_db.Project, "Id", "Name"); 
      ViewBag.ConsultantID = new SelectList(_db.Consultants, "Id", "Name"); 

      return View(); 
     } 

查看:

@model ReportSystem.Models.TimeReports 

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

<h2>Create</h2> 

@using (Html.BeginForm()) { 
    @Html.AntiForgeryToken() 
    @Html.ValidationSummary(true) 

    @Html.HiddenFor(model => model.ConsultantID) 

    <fieldset> 
     <legend>TimeReports</legend> 

     <div class="editor-label"> 
      @Html.LabelFor(model => model.ProjectID, "Project") 
     </div> 
     <div class="editor-field"> 
      @Html.DropDownList("ProjectID", String.Empty) 
      @Html.ValidationMessageFor(model => model.ProjectID) 
     </div> 

     <div class="editor-label"> 
      @Html.LabelFor(model => model.TotalHours) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.TotalHours) 
      @Html.ValidationMessageFor(model => model.TotalHours) 
     </div> 

     <div class="editor-label"> 
      @Html.LabelFor(model => model.TimeType) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.TimeType) 
      @Html.ValidationMessageFor(model => model.TimeType) 
     </div> 

     <div class="editor-label"> 
      @Html.LabelFor(model => model.OnSite) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.OnSite) 
      @Html.ValidationMessageFor(model => model.OnSite) 
     </div> 

     <div class="editor-label"> 
      @Html.LabelFor(model => model.Description) 
     </div> 
     <div class="editor-field"> 
      @Html.TextAreaFor(model => model.Description) 
      @Html.ValidationMessageFor(model => model.Description) 
     </div> 

     <div class="editor-label"> 
      @Html.LabelFor(model => model.Date) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.Date) 
      @Html.ValidationMessageFor(model => model.Date) 
     </div> 

     <p> 
      <input type="submit" value="Create" /> 
     </p> 
    </fieldset> 
} 

<div> 
    @Html.ActionLink("Back to List", "Index") 
</div> 

@section Scripts { 
    @Scripts.Render("~/bundles/jqueryval") 
} 

SQL數據庫:

CREATE TABLE [dbo].[ProjectMembers] (
     [Id]   INT IDENTITY (1, 1) NOT NULL, 
     [ProjectID] INT NOT NULL, 
     [ConsultantID] INT NOT NULL, 
     CONSTRAINT [PK_ProjectMembers] PRIMARY KEY CLUSTERED ([Id] ASC), 
     CONSTRAINT [FK_ProjectMembers_ProjectID] FOREIGN KEY ([ProjectID]) REFERENCES [dbo].[Project] ([Id]), 
     CONSTRAINT [FK_ProjectMembers_ConsultantID] FOREIGN KEY ([ConsultantID]) REFERENCES [dbo].[Consultants] ([Id]) 
    ); 

CREATE TABLE [dbo].[Consultants] (
    [Id]  INT   NOT NULL, 
    [RoleID] INT   NOT NULL, 
    [Name]  NVARCHAR (50) NOT NULL, 
    [PayRoll] DECIMAL (9) NOT NULL, 
    [Password] NVARCHAR (50) NOT NULL, 
    CONSTRAINT [PK_Consultants] PRIMARY KEY CLUSTERED ([Id] ASC), 
    CONSTRAINT [FK_Consultants_RoleID] FOREIGN KEY ([RoleID]) REFERENCES [dbo].[Role] ([Id]) 
); 

CREATE TABLE [dbo].[Project] (
    [Id]   INT   IDENTITY (1, 1) NOT NULL, 
    [Name]   NVARCHAR (50) NOT NULL, 
    [ProjectId] NVARCHAR (50) NOT NULL, 
    [Image]  IMAGE   NULL, 
    [SubProject] NVARCHAR (50) NULL, 
    [Role]   NVARCHAR (50) NOT NULL, 
    [TotalHours] DECIMAL (9, 1) NOT NULL, 
    [ProjectType] NVARCHAR (50) NULL, 
    [Budget]  MONEY   NOT NULL, 
    [Orders]  NVARCHAR (50) NOT NULL, 
    [ProfitMargin] NVARCHAR (50) NOT NULL, 
    [Active]  BIT   NOT NULL, 
    [ProjectStart] DATE   NOT NULL, 
    [ProjectEnd] DATE   NULL, 
    [TimeType]  NVARCHAR (50) NOT NULL, 
    [KickBack]  NVARCHAR (50) NOT NULL, 
    CONSTRAINT [PK_Project] PRIMARY KEY CLUSTERED ([Id] ASC) 
); 
+2

檢查Entity Framework如何生成您的實體,確保主鍵和外鍵按您期待的方式構建。此外,它看起來像你的ProjectMembers表有一個外鍵項目,但你沒有在你的文章中包含該表,是在實體框架?如果沒有,請將其包含在實體框架中,或者如果您沒有使用實體框架,請將該鏈接從實體中刪除。 – Paritosh 2015-03-13 13:06:49

回答

0

由於ProjectMembers表具有主鍵,因此嘗試將find語句中的主鍵傳遞爲var projectMembers = _db.ProjectMembers.Find((int)Session [「Id」]);請參閱https://msdn.microsoft.com/en-gb/data/jj573936.aspx

+0

指定的參數類型'System.Data.Entity.Infrastructure.DbQuery'1 [[ReportSystem.Models.ProjectMembers,ReportSystem,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null]]'無效。只支持標量類型,如System.Int32,System.Decimal,System.DateTime和System.Guid。 var primarykey = _db.ProjectMembers.Where(i => i.Id ==(int)Session [「Id」]); var projectMembers = _db.ProjectMembers.Find(primarykey); 它沒有工作先生。 – 2015-03-13 14:22:51

+1

您正試圖將集合作爲主鍵傳遞,因此出現錯誤。請看我編輯的答案。 – sansh 2015-03-13 15:28:31

相關問題