Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

context.savechanges saves data from back

i have problem with saving data in my database. For example i have this code:

            if (!context.Kategorijas.Any())
        {
            context.Kategorijas.Add(new Kategorija { Naziv = "Hrana" });
            context.Kategorijas.Add(new Kategorija { Naziv = "Igracke" });
            context.Kategorijas.Add(new Kategorija { Naziv = "Odjeca" });
            context.Kategorijas.Add(new Kategorija { Naziv = "Higijena" });
            context.Kategorijas.Add(new Kategorija { Naziv = "Oprema" });
        }

        context.SaveChanges();

My problem is that this data is saved in database, but from back, like this:

Id 1 – Oprema

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Id 2 – Higijena

Id 3 – Odjeca

Id 4 – Igracke

Id 5 – Hrana

How to fix this ? Thank you.

>Solution :

you will have to use save changes after each add, think twice do you really need it

     if (!context.Kategorijas.Any())
        {
         
             context.Kategorijas.Add(new Kategorija { Naziv = "Hrana" });
             context.SaveChanges();
            context.Kategorijas.Add(new Kategorija { Naziv = "Igracke" });
               context.SaveChanges();

           // and so on
        }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading