英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论