Sometimes (in specific case) I can't detach an entity from the context. Why might this be true?

asked 2022-04-01T03:35:10.69+00:00
Robert Gustafson 601 Reputation points

WHAT I HAVE:
Visual Basic 2019, WinForms, Entity Framework (I'm not sure which version; I think 6)

MY ISSUE:
For reasons too long and complex to explain here, I'm occasionally forced by my VB.NET app and entity data model to load in a reference to an entity instance (to different variables) twice while in the data context. Since EF doesn't allow multiple references to 1 entity to be attached simultaneously, I have to detach the 1st variable, using .Entry/.State to change it from EntityState.Unchanged to EntityState.Detached, prior to performing the 2nd query. Normally this works just fine. However, for 1 specific entity type in my app, and under very specific scenarios (that are ostensibly the same as others), the statement to detach instead sets the 1st reference to EntityState.Added instead, triggering either one of 2 exceptions: Either I'm trying to multi-attach the entity to the context, or I'm trying to add it to the database when it's already present.

A surefire way to guarantee detachment is to close the context and re-open it prior to the 2nd query, but that, I suspect, has 2 problems: 1) If the database is big, then performance penalties, and 2) not being able to "transactionalize" changes made after the 1st query and then the 2nd, so that they both succeed or fail together.

Long story short, I need to know if it's a bug in my version of EF (I think it's EF6, but don't hold me to that!), or if there's something wrong with the way the class for that particular entity is set up? (Full disclosure: The classes for the types where detachment works and the 1 where it doesn't always are not fundamentally different, and they all use Partial Class logic to augment them with additional, non-mapped, members.)

Something like this is what I do:

Using db = *context*
   Dim x = *query to get entity instance*
   db.Entry(x).State = EntityState.Detached 'usually, but not invariably, works
   '   some other code
   ...
   Dim y = *query to get collection including previous instance*
   '   some other code
   ...
   db.SaveChanges()
End Using

Please keep any recommendations in VB.NET and as simple as possible.

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,314 questions
Entity Framework 6.0
Entity Framework 6.0
A Microsoft open-source object-database mapper for .NET.
216 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
1,825 questions
{count} votes

1 answer

Sort by: Most helpful
  1. answered 2022-04-04T23:24:29.747+00:00
    Robert Gustafson 601 Reputation points

    I don't get exceptions when I try to detach the entity. Instead, the .Entry/.State statement which is supposed to set it to EntityState.Detached sometimes sets it EntityState.Added instead. That then triggers an exception for double-attachment of the entity or for trying to add a pre-existing entity. But the attempt to detach in and of itself doesn't trigger an exception--it simply malfunctions. The bizarre thing is that this is so for only 1 kind of entity, and only under some circumstances. (BTW, I've tried to circumvent needed to detach x by getting a clone of x instead, which appears to work--but I still need to know why simple detachment isn't always reliable.)

    No comments