user2497476 user2497476 - 2 days ago 5
C# Question

Delete a single record from Entity Framework?

(Reworded)

I have a SQL Server table in Entity Framework named

employ
with a single key column named
ID
.

How do I delete a single record from the table using Entity Framework?

Answer

It's not necessary to query the object first, you can attach it to the context by its id. Like this:

var employer = new Employ { Id = 1 };
ctx.Employ.Attach(employer);
ctx.Employ.Remove(employer);
ctx.SaveChanges();
Comments