0

I want to DELETE some items from my Model which I generated from the database like that:

        db = new TestDBEntities();
        foreach (var item in db.Farbe)
        {
            _model.Add(new Farbe { FarbauswahlNr = item.FarbauswahlNr, Kurztext = item.Kurztext, Ressource = item.Ressource,Vari1 = Convert.ToBoolean(item.Var1) ,Vari2 = item.Vari2 });
        }

I'm showing this Model in a RadGridView and deleting by Selecting and Index per Rightcklick on the mouse like this:

        public void ExecuteDelete(object obj)
        {

            farbliste.Model.Remove(SelectedIndex);

            ListeAktualisieren();

        }

Now the question is how do I Delete something from my Database because if I just Delete from my Model it wont work and that's also not what I want.

Btw some variable are named in German sorry...

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
dennis schütz
  • 383
  • 4
  • 21

1 Answers1

1

you have to save your context. Your context in the first block of code is db.

So:

 db = new TestDBEntities();

 db.entity.Remove(db.entity.Find(SelectedIndex));

 db.SaveChanges();

Entity is the name of your table/object from entity frramework. I would also suggest wraping this is a using block for the db

J King
  • 4,108
  • 10
  • 53
  • 103
  • Well, that was mainly correct but i still cant delete my rows. There is no Error Message or something... but still there'r all rows after delete. But they should be gone of the Database right? – dennis schütz Mar 18 '13 at 07:19
  • Ahh ... SaveChanges is throwing an exception after delete :( – dennis schütz Mar 18 '13 at 08:04
  • What is the exception and can you post the code you are now using – J King Mar 18 '13 at 14:23
  • intresting. i figuerd out the Problem. My entity Framwork was Kind of Buggy. My Tabels where shown in the EnitiyModel but even if im updating nothing changed. But after i deletet the Table and loaded it again from the database the Probplem was gone. – dennis schütz Mar 20 '13 at 07:45