Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
545 views
in Technique[技术] by (71.8m points)

entity framework - 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();
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...