0

I am trying to create a new employee in MVC 5. I used CRUD to generate views and models. I edited a few lines of code and I get this error:

Cannot insert explicit value for identity column in table 'Table' when IDENTITY_INSERT is set to OFF.

Where I got this:

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");
    }

My model class:

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; }
}

Create form view:

@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' class is employee and he is inherits from 'Osoba' which is Person in English. I dont know where I trying to put this id in my code.

ekad
  • 14,436
  • 26
  • 44
  • 46
TjDillashaw
  • 787
  • 1
  • 12
  • 29
  • possible duplicate of [Cannot insert explicit value for identity column in table 'table' when IDENTITY\_INSERT is set to OFF](http://stackoverflow.com/questions/1334012/cannot-insert-explicit-value-for-identity-column-in-table-table-when-identity) – Oscar Jun 15 '15 at 20:16
  • Also, see this: http://blog.robertobonini.com/2014/10/09/entity-framework-with-identity-insert/ – Oscar Jun 15 '15 at 20:16
  • You have osoba and pracownik as parameters, where do they come from? Do you explicitly set an ID there? Are they result of a previous query? – Adriano Repetti Jun 15 '15 at 20:18
  • They come from a Create form view. I edit the post. – TjDillashaw Jun 15 '15 at 20:26
  • 1
    You get this error when the DBML file (the database model) "thinks" it should set the value while the database generates the identity value. You could update the model so it has the correct definition which should solve the problem – Luc Jun 15 '15 at 20:33
  • But what i must change in this model ? – TjDillashaw Jun 15 '15 at 20:41

1 Answers1

3

You are trying to save an object to the database with an explicit ID set by you while the database is expecting to generate that value itself. That is the Id_osoby property in your object is set to something other than 0 and it is not identified to the EF framework as an identity field. If you want the Id to be generated by the database as your post suggests, then the corresponding property in the Object should be decorated with the [Key] attribute.

BSG
  • 2,084
  • 14
  • 19
  • It works! I add [Key] atrribute to Osoba. But now when i create "Pracownik" it creates duplicate records in Osoba. Why? – TjDillashaw Jun 16 '15 at 08:44
  • 1
    In order to avoid the duplication you must attach the related entity to the context: context.Pracowniks.Attach(myOsoba.Pracownik ); context.Osobas.Add(myOsoba); – BSG Jun 16 '15 at 08:49