5

I am trying to delete record from table store but I having problem of recognizing DeleteObject in my code. I have reference

using System.Linq; using System.Data.Entity; using System.Data.Objects; 

but it still not working. I am using MVC 4 using Visual Studio 2012.

public void Delete() { using (var db = new AppContext()) { var query_D = (from b in db.Stores where b.storeID == 1 select b).First(); db.DeleteObject(query_D); db.SaveChanges(); } } 

thanks in advance

2
  • I guess you're using the EF5? Commented Jan 29, 2013 at 13:47
  • db.Stores.Remove or db.Stores.Delete or anything else that is in your context version and same logic ... Commented Jan 29, 2013 at 13:51

3 Answers 3

11

I realized that you're using the MVC 4 with VS2012, and by default the Entity Framework version is 5.

Now the way you delete is from EF4.

Here's the proper way to delete using the EF5

using (var db= new AppContext(ConnectionStr)) { try { con.Configuration.AutoDetectChangesEnabled = false; var o = new Store { Id = 1 }; db.Stores.Attach(o); db.Stores.Remove(o); db.SaveChanges(); } catch (Exception ex) { throw new Exception(ex.InnerException.Message); } finally { con.Configuration.AutoDetectChangesEnabled = true; } } 
Sign up to request clarification or add additional context in comments.

1 Comment

i have managed to worked with remove method as well ... thanks as well
8

Just use

db.Entry(query_D).State = System.Data.EntityState.Deleted; 

Comments

0

You should try :

db.Stores.Remove(query_D); db.SaveChanges(); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.