传递的主键值的数量必须与实体上定义的主键值的数量相匹配。 ASP.NET MVC实体框架

时间:2015-03-13 12:56:47

标签: c# asp.net-mvc entity-framework

因此,我试图启用项目的下拉列表,具体取决于用户是否已分配项目。但是我继续得到你在标题上看到的错误......我正在使用带有实体框架的ASP.NET MVC 5

这里有一些代码:

控制器:

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

1 个答案:

答案 0 :(得分:0)

由于ProjectMembers表具有主键,因此尝试将find语句中的主键作为var projectMembers = _db.ProjectMembers.Find((int)Session [“Id”]);请参阅https://msdn.microsoft.com/en-gb/data/jj573936.aspx