This question is locked. New answers and comments are not allowed.
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?
Can someone explain to me what I am doing wrong?
6 Answers, 1 is accepted
0
Accepted
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
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
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
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;
}
Cheers
0
Hi Miladin,
I hope this helps. Do not hesitate to contact us back if you need any further assistance.
Kind regards,
Dimitar Tachev
the Telerik team
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
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
Hi Miladin,
Dimitar Tachev
the Telerik team
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.
Dimitar Tachev
the Telerik team
Using Encrypted Connection Strings with Telerik OpenAccess ORM. Read our latest blog article >>