【发布时间】: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 放在我的代码中的什么地方。
【问题讨论】:
-
你有 osoba 和 pracownik 作为参数,它们是从哪里来的?您是否在此处明确设置了 ID?它们是先前查询的结果吗?
-
它们来自创建表单视图。我编辑帖子。
-
当 DBML 文件(数据库模型)“认为”它应该在数据库生成标识值时设置值时,您会收到此错误。您可以更新模型,使其具有应该解决问题的正确定义
标签: sql asp.net asp.net-mvc