Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i delete single record from table using EF 6.1.1

I am using Entity Framework 6.1.1.

I am deleting single record from table as following but i am not sure whether its the only way or could further rewrite it in an efficient way.

Can someone share comments?

Reason: I am asking because many solutions in earlier posts are referring to EF 4.0 and not using the latest version 6.1.1.

Guid studentId = student.Id;
StudentReportDetail stuDetails = _context.StudentReportDetail.Find(studentId);
if (stuDetails != null)
{
    _context.StudentReportDetail.Remove(stuDetails);
    _context.SaveChanges();
}
like image 702
immirza Avatar asked Jul 28 '15 09:07

immirza


People also ask

How do I delete a record in Entity Framework?

Delete a Record In Connected Scenario, you can use the Remove or RemoveRange method to mark the record as Deleted . In Disconnected Scenario, you can attach it to the context and set its state as Deleted . Calling SaveChanges will send the delete query to the database.

How do I use RemoveRange in Entity Framework?

RemoveRange() method attaches a collection of entities with Deleted state, which in turn will execute the DELETE command for all entities on SaveChanges() . Adding or removing entities using the AddRange and RemoveRange methods improves the performance.

How do I delete multiple rows in Entity Framework?

RemoveRange. The RemoveRange method is used for deleting multiple objects from the database in one method call. The following code deletes a large number of records from the database using RemoveRange.

How do I delete a table in Entity Framework?

To remove a table, simply remove the corresponding DbSet<MyClass> and any references to that class in other parts of your model and EF will add a DropTable to the migration automatically. If you are no longer using the class for non-Entity Framework purposes you can delete it.


1 Answers

There are no changes about how to delete an entity between EF 4 and EF 6. To delete an entity using Entity Framework, you need to use the Remove method on DbSet. Remove works for both existing and newly added entities.

  • Calling Remove on an entity that has been added but not yet saved to the database will cancel the addition of the entity. The entity is removed from the change tracker and is no longer tracked by the DbContext.

  • Calling Remove on an existing entity that is being change-tracked will register the entity for deletion the next time SaveChanges is called.

Deleting with loading from the database

As the example you show in your question, you need to load first the existing entity from your context to delete it. If you don't know the Id, you can execute a query as I show below to find it first:

    var report= (from d in context.StudentReportDetail
               where d.ReportName == "Report"
               select d).Single();

    context.StudentReportDetail.Remove(report);
    context.SaveChanges();

Deleting without loading from the database

If you need to delete an entity, but it’s not already in memory, it’s a little inefficient to retrieve that entity from the database just to delete it. If you know the key of the entity you want to delete, you can attach a stub that represents the entity to be deleted, and then delete this stub. A stub is an instance of an entity that just has the key value assigned. The key value is all that’s required for deleting entities.

var toDelete = new StudentReportDetail {Id = 2 };

context.StudentReportDetail.Attach(toDelete);
context.StudentReportDetail.Remove(toDelete);
context.SaveChanges();

Other way could be changing the entity's state to Deleted.DbContext has methods called Entry and Entry<TEntity>, these methods get a DbEntityEntry for the given entity and provide access to the information about the entity and return a DbEntityEntry object able to perform the action on the entity. Now you can perform the delete operation on the context by just changing the entity state to EntityState.Deleted:

var toDelete = new StudentReportDetail {Id = 2 };
context.Entry(toDelete).State = EntityState.Deleted;  
context.SaveChanges();  

Using a 3rd party library

There is another way but is using a 3rd party library, EntityFramework Plus, there is a nugget package you can install. You can use the batch delete operation:

context.StudentReportDetail
    .Where(u => u.Id== stuDetails)
    .Delete();
like image 70
octavioccl Avatar answered Sep 27 '22 20:09

octavioccl