Delete a single record from Entity Framework?

c# entity-framework sql-server

Question

In Entity Framework, I have a SQL Server table with the nameemploy using a single named key columnID .

How can I use Entity Framework to remove a single record from the table?

1
193
2/2/2018 6:45:36 AM

Accepted Answer

ZZZ_tmp
358
2/20/2018 6:26:35 PM

Popular Answer

You may utilizeSingleOrDefault to locate a single item that fits your requirements, send that on to theRemove use of your EF table technique.

var itemToRemove = Context.Employ.SingleOrDefault(x => x.id == 1); //returns a single item.

if (itemToRemove != null) {
    Context.Employ.Remove(itemToRemove);
    Context.SaveChanges();
}


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