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

时间:2015-06-15 20:12:34

标签: sql asp.net asp.net-mvc

我正在尝试在MVC 5中创建一个新员工。我使用CRUD生成视图和模型。我编辑了几行代码,我收到了这个错误:

  

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

我得到了这个:

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'上课是员工,他继承自Osoba&#39;这是英文的人。我不知道在哪里尝试将此id放入我的代码中。

1 个答案:

答案 0 :(得分:2)

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