【问题标题】:IDENTITY_INSERT is set to OFF in MVC applicationIDENTITY_INSERT 在 MVC 应用程序中设置为 OFF
【发布时间】:2015-06-15 20:12:33
【问题描述】:

我正在尝试在 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”,即英语中的 Person。我不知道我试图把这个 id 放在我的代码中的什么地方。

【问题讨论】:

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


【解决方案1】:

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

【讨论】:

  • 有效!我将 [Key] 属性添加到 Osoba。但是现在当我创建“Pracownik”时,它会在 Osoba 中创建重复记录。为什么?
  • 为了避免重复,您必须将相关实体附加到上下文: context.Pracowniks.Attach(myOsoba.Pracownik ); context.Osobas.Add(myOsoba);
猜你喜欢
  • 2013-06-09
  • 1970-01-01
  • 2021-08-27
  • 2015-10-18
  • 1970-01-01
  • 2011-10-02
  • 2018-05-27
  • 2012-04-24
  • 2010-10-01
相关资源
最近更新 更多