繁体   English   中英

在MVC应用程序中IDENTITY_INSERT设置为OFF

[英]IDENTITY_INSERT is set to OFF in MVC application

我试图在MVC 5中创建新员工。我使用CRUD生成视图和模型。 我编辑了几行代码,然后出现此错误:

当IDENTITY_INSERT设置为OFF时,无法在表'Table'中为标识列插入显式值。

我从哪里得到的:

public ActionResult Create([Bind(Include = "Imie,Nazwisko,Pesel,Data_zatrudnienia,Data_zwolnienia,Pensja,Wyksztalcenie")] Osoba osoba,Pracownik pracownik)
{
    if (ModelState.IsValid)
    {
        db.Osoba.Add(osoba);
        db.Pracownik.Add(pracownik);

        db.SaveChanges();
        return RedirectToAction("Index");
    }

我的模特班:

public partial class Pracownik
{
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int Id_osoby { get; set; }
    public System.DateTime Data_zatrudnienia { get; set; }
    public Nullable<System.DateTime> Data_zwolnienia { get; set; }
    public double Pensja { get; set; }
    public string Wyksztalcenie { get; set; }

    public virtual Osoba Osoba { get; set; }
    public virtual Pracownik_biurowy Pracownik_biurowy { get; set; }
    public virtual Przewodnik Przewodnik { get; set; }
}

public partial class Osoba
{
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int Id_osoby { get; set; }
    public string Imie { get; set; }
    public string Nazwisko { get; set; }
    public string Pesel { get; set; }

    public virtual Pracownik Pracownik { get; set; }
}

创建表单视图:

@model BiuroPrototyp.Models.Pracownik

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>

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

    <div class="form-horizontal">
        <h4>Pracownik</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.Osoba.Imie, htmlAttributes: new {@class = "control-label col-md-2"})
            <div class="col-md-10">
                @Html.EditorFor(model => model.Osoba.Imie, new {htmlAttributes = new {@class = "form-control"}})
                @Html.ValidationMessageFor(model => model.Osoba.Imie, "", new {@class = "text-danger"})
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.Osoba.Nazwisko, htmlAttributes: new {@class = "control-label col-md-2"})
            <div class="col-md-10">
                @Html.EditorFor(model => model.Osoba.Nazwisko, new {htmlAttributes = new {@class = "form-control"}})
                @Html.ValidationMessageFor(model => model.Osoba.Nazwisko, "", new {@class = "text-danger"})
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Osoba.Pesel, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Osoba.Pesel, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Osoba.Pesel, "", new { @class = "text-danger" })
            </div>
        </div>
            <div class="form-group">
                @Html.LabelFor(model => model.Data_zatrudnienia, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.Data_zatrudnienia, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.Data_zatrudnienia, "", new { @class = "text-danger" })
                </div>
            </div>



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

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

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

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

'Pracownik'类是雇员,他是英语中“ Person”的“ Osoba”的继承人。 我不知道我在哪里尝试将此ID放入代码中。

您正在尝试使用数据库设置的显式ID将对象保存到数据库,而数据库本身希望生成该值。 也就是说,您对象中的Id_osoby属性设置为0以外的值,并且EF框架未将其标识为标识字段。 如果您希望按照帖子的建议由数据库生成ID,则应使用[Key]属性修饰Object中相应的属性。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM