Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法编辑主键_C#_Sql_Entity Framework_Razor_Asp.net Mvc 5 - Fatal编程技术网

C# 无法编辑主键

C# 无法编辑主键,c#,sql,entity-framework,razor,asp.net-mvc-5,C#,Sql,Entity Framework,Razor,Asp.net Mvc 5,我正在用MVC和ASP.NET制作一个c应用程序。我有一个年实体,它是一个年表和一个主ID,称为YearID。但当我去编辑YearID时,它不会改变。不能编辑主键吗?有办法解决这个问题吗?YearID必须是主键 控制器代码: [HttpPost] [ValidateAntiForgeryToken] public ActionResult Edit([Bind(Include="YearID")] Year year) { try { if(ModelState.

我正在用MVC和ASP.NET制作一个c应用程序。我有一个年实体,它是一个年表和一个主ID,称为YearID。但当我去编辑YearID时,它不会改变。不能编辑主键吗?有办法解决这个问题吗?YearID必须是主键

控制器代码:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include="YearID")] Year year)
{
    try
    {
        if(ModelState.IsValid)
        {
            db.Entry(year).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }
    }
    catch (DataException /* dex */)
    {
        //Log the error (uncomment dex variable name and add a line here to write a log.
        ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator.");
    }
    return View(year);
}
年份型号:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations.Schema;

namespace Utility.Models
{
    public class Year
    {
        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        public int YearID { get; set; }

        public virtual ICollection<Week> Weeks { get; set; }
    }
}
编辑html: @实用新型

@{
    ViewBag.Title = "Edit";
}

<h2>Edit</h2>


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

    <div class="form-horizontal">
        <h4>Year</h4>
        <hr />
        @Html.ValidationSummary(true)
        @Html.HiddenFor(model => model.YearID)

        <div class="form-group">
            @Html.LabelFor(model => model.YearID, new { @class = "control-label col-md-2"             })
            <div class="col-md-10">
                @Html.EditorFor(model => model.YearID)
                @Html.ValidationMessageFor(model => model.YearID)
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Save" class="btn btn-default" />
            </div>
        </div>
    </div>
}

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

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

如果需要更多的代码,请告诉我。

此编辑是否与其他程序配合使用?是的,它与我的其他几个程序配合使用,唯一的区别是我正在尝试编辑主键。当您尝试编辑主键时是否抛出错误?如果您试图将其值更改为数据库中已经是主键值的值,则会失败。AFAIK您无法在ASp MVC中真正更改PK。最重要的问题是:你为什么要这么做?我想你误解了PK/FK的概念。请注意,更改PK需要同时更改所有FK实例。实体框架通过主键将实体链接到上下文。一旦使用该主键创建了记录,就不能编辑该主键,因为这会使其成为完全不同的记录。相反,您必须使用新的主键创建新记录并删除旧记录。或者,您可以将模型与EF分离,然后根据表单中YearID的值创建新记录或选择现有记录。