我怎么可以无视我的实体框架code一DbUpdateConcurrencyException?我的、实体、框架、我怎么

2023-09-04 22:50:54 作者:天镜

有什么办法,我可以告诉EF不用担心的行数删除更新做或不做什么?

我想从数据库中删除一行,但由于该行不存在,EF抛出一个异常: DbUpdateConcurrencyException ..说的 0行受到影响的。这是正确的 - >没有行被删除。但是,这是完全正常......导致没有数据。

我真的不希望做一个往返到数据库,看看是否行存在..如果是这样..然后尝试将其删除。

如果我尝试在的try / catch 块吞下异常,那么其余项目将被删除没有得到发送到数据库,当我尝试以的SaveChanges() ...这是坏的。

如:

 删除(新的Foo(1));
删除(新的Foo(2));
删除(新的Foo(3));
保存更改(); //<  - 抛出异常。

// DB跟踪:DELETE FROM美孚其中id = 1;
 

和多数民众赞成它..没有一丝显示记录2或3想把你删除..因为异常停止一切:(

任何想法?

更新

如何删除工作?这里的code ......(简体和强类型)

 公共无效删除(富富)
{
    如果(FOO == NULL)
    {
        抛出新ArgumentNullException(富);
    }

    美孚attachedEntity = Context.Set&其中;富>()Local.FirstOrDefault(X => x.Id大于0);

    如果(attachedEntity!= NULL)
    {
        //实体已经在对象图形 - 删除实体。
        Context.Set<富>()删除(attachedEntity)。
    }
    其他
    {
        //实体不在对象图,连接和设置的EntityState为删除。
        Context.Entry(富).State = EntityState.Deleted;
    }
}
 
从零开始学前端 HTML框架和VS Code安装 今天你学习了吗 Day01

解决方案

我觉得英孚的行为是正确的 - 只是你必须只对其中的present在数据库对象执行命令。它不是像情景:我会尝试,我们将看到...。如果你不能确保对象存在于数据库,你不想做往返(我认为这是最好的办法,因为删除分离的对象可以有其他的几个陷阱,特别是如果对象参与了独立的关联),你应该使用 DbContext.Database.SqlCommand 和运行存储过程。

正确的方法来处理 DbUpdateConcurrencyException 描述此处 =>每个异常后,您应该解决冲突分析(在你的情况下,它是指从的DbContext删除有问题的实体)和的SaveChanges 再次执行。

Is there any way I can tell EF to not worry about the number of rows a DELETE or UPDATE do or don't do?

I'm trying to delete a row from the database, but because the row doesn't exist, EF throws an exception: DbUpdateConcurrencyException .. saying 0 rows were affected. This is right -> no rows were deleted. But that's totally fine .. cause there's no data.

I don't really want to do a round-trip to the DB to see if that row exists .. and if so .. then try and delete it.

If i try and swallow the exception in a try / catch block, then the rest of the items to be deleted do NOT get sent to the db, when I try to SaveChanges() ... which is bad.

eg.

Delete(new Foo(1));
Delete(new Foo(2));
Delete(new Foo(3));
SaveChanges(); // <-- Throws the exception.

// DB Trace : DELETE FROM Foo WHERE Id = 1;

and thats it.. there's no trace showing record 2 or 3 trying to get deleted .. because the exception stops everything :(

Any ideas?

UPDATE

How does Delete work? Here's the code... (simplified and strongly typed)

public void Delete(Foo foo)
{
    if (foo == null)
    {
        throw new ArgumentNullException("foo");
    }

    Foo attachedEntity = Context.Set<Foo>().Local.FirstOrDefault(x => x.Id > 0);

    if (attachedEntity != null)
    {
        // Entity already in object graph - remove entity.
        Context.Set<Foo>().Remove(attachedEntity);
    }
    else
    {
        // Entity not in object graph, attach and set EntityState to Deleted.
        Context.Entry(foo).State = EntityState.Deleted;
    }
}

解决方案

I think the behavior of EF is correct - simply you must execute commands only for objects which are present in DB. It is not for scenarios like: "I will try it and we will see...". If you can't be sure that object exists in DB and you don't want to do round trip (which I think is the best idea because deleting detached object can have several other pitfalls especially if object participates in independent associations) you should use DbContext.Database.SqlCommand and run store procedure.

The correct way to handle DbUpdateConcurrencyException is described here => After each exception you should resolve confilicts (in your case it means remove problematic entity from DbContext) and execute SaveChanges again.