如何使用EF 6.1.1从表中删除单个记录 [英] How do i delete single record from table using EF 6.1.1
问题描述
我正在使用实体框架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.
有人可以分享评论?
原因:我是问,因为早期帖子中的许多解决方案都是指EF 4.0而不是使用最新版本6.1.1。
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();
}
推荐答案
删除EF 4和EF 6之间的实体。要使用Entity Framework删除实体,您需要在 DbSet $上使用
删除
方法C $ C>。 删除
适用于现有和新添加的实体。
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.
-
调用
删除
保存
到数据库将取消添加实体。实体是
从更改跟踪器中删除,不再被
DbContext
跟踪。
调用删除
在正在更改跟踪的现有实体上
将在下次注册该实体删除 SaveChanges
被调用
。
Calling Remove
on an existing entity that is being change-tracked
will register the entity for deletion the next time SaveChanges
is
called.
数据库
作为您在问题中显示的示例,您需要先从上下文加载现有实体以将其删除。如果您不知道 Id
,您可以执行一个查询,如下所示:首先找到它:
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();
删除而不从数据库加载
如果您需要删除一个实体,但它尚未在内存中,那么从数据库中检索该实体只是为了将其删除,这样做有点低效。如果您知道要删除的实体的密钥,则可以附加表示要删除的实体的存根,然后删除该存根。一个存根是一个实体,它只分配了键值。
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();
其他方法可能会将实体的状态更改为已删除
。 DbContext
具有名为 Entry
和 Entry< TEntity>
,这些方法得到给定实体的 DbEntityEntry
,并提供对实体的信息的访问,并返回一个 DbEntityEntry
EntityState.Deleted :
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();
使用第三方库
另外还有一种方法是使用第三方库, EntityFramework Plus 有一个可以安装的块包。您可以使用批量删除操作:
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();
这篇关于如何使用EF 6.1.1从表中删除单个记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!