0
votes

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();
    }
1
[DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int ID { get; set; } - Lukasz Szozda
Thanks for response. But it did'nt helped at all :/. - Michał Filipowicz
You shouldn't define ID in Klient because it will already inherit it from Osoba - juharr
Yeah, I also thought about it, but it didn't change anything. I was still getting the same error when "Klient" class had not "ID". - Michał Filipowicz
Where it says ctx.Osoba.Add(tmp);; should that be doing ctx.Klient.Add(tmp);, since tmp is a Klient? - Richardissimo

1 Answers

0
votes

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.