Entity Framework – 使用主键删除记录

huangapple go评论57阅读模式
英文:

Entity Framework - delete a record with the primary key

问题

是的,我知道可以执行以下操作:

using (var context = new MyDbContext())
{
    var entity = new MyEntity { Id = id };
    context.Entry(entity).State = EntityState.Deleted;
    context.SaveChanges();
}

但我更喜欢避免不必要的加载记录的开销。

英文:

Is there a way to delete a record by its primary key?

Yes I know I can do the following:

using (var context = new MyDbContext())
{
    var entity = context.MyEntity.Find(id);
    context.MyEntity.Remove(entity);
    context.SaveChanges();
}

But I prefer to not incur the unnecessary overhead of loading the record first.

I am aware of this question but it is 11 years old and I'm hoping there's been a solution for this added since then.

答案1

得分: 0

Nope, still about the quickest way to delete an entity without the round-trip. However there is one important detail if using something like an injected DbContext:

For instance, the following code is perfectly fine with a scoped DbContext:

使用以下代码,具有作用域 DbContext 完全正常:

using (var context = new AppDbContext())
{
    var entityToDelete = new MyEntity { Id = idToDelete };
    context.Attach(entityToDelete);
    context.MyEntities.Remove(entityToDelete);
    context.SaveChanges();
}

But with an injected DbContext, attaching an entity can result in an exception if the DbContext happens to be already tracking an entity with that ID. This can result in situational runtime exceptions. The correct approach without a round trip with an injected DbContext would be:

但是,使用注入的 DbContext,如果 DbContext 已经跟踪了具有该 ID 的实体,则附加实体可能会导致异常。这可能会导致情景运行时异常。在没有使用往返操作的情况下,使用注入的 DbContext 的正确方法如下:

var entityToDelete = context.MyEntities.Local.FirstOrDefault(x => x.Id == idToDelete);
if(entityToDelete == null)
{
    entityToDelete = new MyEntity { Id = idToDelete };
    context.Attach(entityToDelete);
}
context.MyEntities.Remove(entityToDelete);
context.SaveChanges();

We need to check the local tracking cache prior to attaching an entity. If we are tracking that desired entity, we remove that instance, otherwise we can go ahead and attach a new dummy record.

我们需要在附加实体之前检查本地跟踪缓存。如果我们正在跟踪所需的实体,我们会移除该实例,否则我们可以继续附加一个新的虚拟记录。

英文:

Nope, still about the quickest way to delete an entity without the round-trip. However there is one important detail if using something like an injected DbContext:

For instance, the following code is perfectly fine with a scoped DbContext:

using (var context = new AppDbContext())
{
    var entityToDelete = new MyEntity { Id = idToDelete };
    context.Attach(entityToDelete);
    context.MyEntities.Remove(entityToDelete);
    context.SaveChanges();
}

But with an injected DbContext, attaching an entity can result in an exception if the DbContext happens to be already tracking an entity with that ID. This can result in situational runtime exceptions. The correct approach without a round trip with an injected DbContext would be:

var entityToDelete = context.MyEntities.Local.FirstOrDefault(x => x.Id == idToDelete);
if(entityToDelete == null)
{
    entityToDelete = new MyEntity { Id = idToDelete };
    context.Attach(entityToDelete);
}
context.MyEntities.Remove(entityToDelete);
context.SaveChanges();

We need to check the local tracking cache prior to attaching an entity. If we are tracking that desired entity, we remove that instance, otherwise we can go ahead and attach a new dummy record.

huangapple
  • 本文由 发表于 2023年6月5日 22:50:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76407645.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定