Delete record using entity framework

c# entity-framework-6 vb.net

Question

I am having trouble deleting an entity -

Using db As Context = New Context
        Dim car = db.Car.First(Function(x) x.ID = txtCarID.text)
        db.Entry(car).State = Data.Entity.EntityState.Deleted
End Using

I have tried many other syntax, here is another -

 Using db As Context = New Context
        Dim car = new Car With {.ID = txtCarID.text}
        db.Car.Attach(car)
        db.Car.Remove(car)
End Using

I have received no errors but the record is never deleted.

Where am I going wrong?

Thanks,

1
0
3/22/2015 6:34:00 AM

Accepted Answer

You need to call SaveChanges method of DbContext after you made the changes. Otherwise changes will not be committed to databse.

1
3/22/2015 6:20:57 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow