With entity framework, how do you remove an object by id?

entity entity-framework

Question

With entity framework, it seems that I must obtain an object before deleting it, as seen below.

var customer = context.Customers.First(c => c.Id == 1);

context.DeleteObject(customer);

context.Savechanges();

Thus, I must access the database twice. Is there a simpler method?

1
96
11/21/2018 9:42:25 AM

Popular Answer

Delete is an action in Entity Framework 6 that isRemove . Here's an illustration

Customer customer = new Customer () { Id = id };
context.Customers.Attach(customer);
context.Customers.Remove(customer);
context.SaveChanges();
84
1/20/2015 4:33:49 PM


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