question

RobertGustafson-1682 avatar image
0 Votes"
RobertGustafson-1682 asked RobertGustafson-1682 commented

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

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.














dotnet-visual-basicwindows-formsdotnet-entity-framework
· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@RobertGustafson-1682, Welcome to Microsoft Q&A, I want to know if you meet any error or exceptions when you try to detach the entity from the context. Based on your description, it seems that you didn't provide it. Also, Please refer to the following link to use another method to detach the entity from the context,
How to detach entities from EF Code First contexts


0 Votes 0 ·

I'm using Model-First, since it's easier to start with the conceptual model, given that my databases aren't inherited from earlier apps.

0 Votes 0 ·

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.)

0 Votes 0 ·

Have you tried using AsNoTracking ?

0 Votes 0 ·

What does that do? And what if I need to track T certain points?

0 Votes 0 ·

1 Answer

RobertGustafson-1682 avatar image
0 Votes"
RobertGustafson-1682 answered RobertGustafson-1682 rolled back

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.)






5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.