This is a migrated thread and some comments may be shown as answers.

The instance is transient

6 Answers 341 Views
Getting Started
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Erik
Top achievements
Rank 1
Erik asked on 11 Oct 2012, 04:51 PM
Sometimes, but not always, when I do a dbContext.SaveChanges, I get an InvalidOperationException: "The instance is transient"
Can someone explain to me what I am doing wrong?

6 Answers, 1 is accepted

Sort by
0
Accepted
Ady
Telerik team
answered on 12 Oct 2012, 01:07 PM
Hello Erik,

 The exception is thrown when attempting to delete an object (context.Delete) that was has no context associated with it - maybe it was never added to the context ?
Can you review the code for types for which you get this exception?

All the best,
Ady
the Telerik team
Telerik OpenAccess ORM Meets ASP.NET Web API. Read more.
0
Erik
Top achievements
Rank 1
answered on 12 Oct 2012, 05:27 PM
Thanks Ady, your reply put me on the right track, in a 2nd save operation I was deleting the same object a second time, doh!
0
Miladin
Top achievements
Rank 1
answered on 04 Apr 2013, 07:14 AM
Hi everybody,

Maybe this is not the right place to ask this question, but I spent last hour (or more) trying to find answer on this (Telerik ORM) forum with no success and this thread is closest to my needs :) so here it is:
Is there any property and/or function which can tell me if object is 'transient'?

As far as I am concerned I resolved this in not so beautiful way with try-catch
try
{
    Model.Delete(someEntity);
}
catch (InvalidOperationException ex)
{
    if (!ex.Message.Contains("transient"))
        throw ex;
}
From code above U can see that I had problems with deleting 'transient' objects and I would like to remove that ugly :) try-catch from my code and make it more desirable :)

Cheers
0
Dimitar Tachev
Telerik team
answered on 04 Apr 2013, 02:11 PM
Hi Miladin,

In general you could avoid getting this exception by checking the object state.

The corresponding state for a transient object is the MaskNoMask one - an object becomes in this state when it is not managed by a context.

The following code snipped is demonstrating this approach.

ObjectState objectState = Model.GetState(someEntity);
if (objectState != ObjectState.MaskNoMask)
{
    Model.Delete(someEntity);
}


I hope this helps. Do not hesitate to contact us back if you need any further assistance.

Kind regards,
Dimitar Tachev
the Telerik team
Using Encrypted Connection Strings with Telerik OpenAccess ORM. Read our latest blog article >>
0
Miladin
Top achievements
Rank 1
answered on 04 Apr 2013, 04:24 PM
Thanks for quick reply.

One more thing - I think (and people will probably agree with me on this) that U should probably find a way to put stuff like this in docs, cause thats the 1st place were I've tried to find it :)

Cheers
0
Dimitar Tachev
Telerik team
answered on 09 Apr 2013, 10:52 AM
Hi Miladin,

 
Thank you for your feedback.

The object states were not clearly described in our current resources but I already addressed this issue and you could find them updated in a future version of our online documentation.

All the best,
Dimitar Tachev
the Telerik team
Using Encrypted Connection Strings with Telerik OpenAccess ORM. Read our latest blog article >>
Tags
Getting Started
Asked by
Erik
Top achievements
Rank 1
Answers by
Ady
Telerik team
Erik
Top achievements
Rank 1
Miladin
Top achievements
Rank 1
Dimitar Tachev
Telerik team
Share this question
or