简体   繁体   中英

SqlException: Cannot insert explicit value for identity column in table

At first I'm sorry for using my native language in Code, but it's my University project and our project Leader ordered us to write like this. I'm working on Database project using Entity Framework and C#. In short, I've created class named "Osoba" and "Klient" class which inherits from "Osoba". Problem is that when I'm trying to add new "Klient" to database I'm still getting error as following:

System.Data.Entity.Infrastructure.DbUpdateException: „An error occurred while updating the entries. See the inner exception for details.”

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

I've researched similar problems in web, but all of them were appearing because of "hard-coding" ID while adding new object to table.. and I'm actually not doing this.

Here is Osoba class:

[Table("Osoba")]
public class Osoba
{
    public int ID { get; set; }
    public string Imie { get; set; }
    public string Nazwisko { get; set; }
    public string Telefon { get; set; }
    public string Adres { get; set; }
    public string Mail { get; set; }
    public int IloscTransakcji { get; set; }
    public string Typ { get; set; }

    public override string ToString()
    {
        return "Imie: " + Imie + "\t Nazwisko: " + Nazwisko + "\t Adres: " + Adres;
    }
}

Klient class:

[Table("Klient")]
public class Klient: Osoba
{
    public int ID { get; set; }
    public string Pracownik { get; set; }
    public int Sprzedane { get; set; }
    public int Kupione { get; set; }
    public string Preferencje { get; set; }

    public override string ToString()
    {
        return "Obslugujacy pracownik: " + Pracownik + "\t Sprzedane: " + Sprzedane.ToString() + "\t Kupione: " + Kupione.ToString();
    }
}

My Database Context:

 public class BazyDanychContext : DbContext
{
    public BazyDanychContext() : base("ProjektBD8")
    {
    }

    public DbSet<Osoba> Osoba { get; set; }
    public DbSet<Klient> Klient { get; set; }
    public DbSet<Pracownik> Pracownik { get; set; }
    public DbSet<Nieruchomosc> Nieruchomosc { get; set; }
    public DbSet<Biuro> Biuro { get; set; }
    public DbSet<Dom> Dom { get; set; }
    public DbSet<Grunt> Grunt { get; set; }
    public DbSet<Hala> Hala { get; set; }
    public DbSet<Mieszkanie> Mieszkanie { get; set; }
    public DbSet<Spotkanie> Spotkanie { get; set; }
    public DbSet<Umowa> Umowa { get; set; }
} 

And finally here is how I'm adding new Klient to database:

private void KlientAdd_Click(object sender, RoutedEventArgs e)
    {
        using (var ctx = new BazyDanychContext())
        {

            Klient tmp = new Klient { Imie = KlientImie.Text, Nazwisko = KlientNazwisko.Text, Telefon = KlientTelefon.Text, Adres = KlientAdres.Text, Mail = KlientMail.Text, IloscTransakcji = Int32.Parse(KlientIloscTransakcji.Text), Typ = "Klient" , Pracownik = KlientPracownik.Text, Sprzedane = Int32.Parse(KlientSprzedane.Text), Kupione = Int32.Parse(KlientKupione.Text), Preferencje = KlientPreferencje.Text };
            ctx.Osoba.Add(tmp);
            ctx.SaveChanges();
        }
        InitTabs();
    }

So, final solution for me was removing all migrations in my project. After dropping my Database, removing all migrations and then recreate Database without any migrations in my project it finally worked. Thank you for all your suggestions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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