The instance is transient

7 posts, 1 answers
  1. Erik
    Erik avatar
    36 posts
    Member since:
    Jun 2012

    Posted 11 Oct 2012 Link to this post

    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?
  2. Answer
    Ady
    Admin
    Ady avatar
    589 posts

    Posted 12 Oct 2012 Link to this post

    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.
  3. DevCraft banner
  4. Erik
    Erik avatar
    36 posts
    Member since:
    Jun 2012

    Posted 12 Oct 2012 Link to this post

    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!
  5. Miladin
    Miladin avatar
    3 posts
    Member since:
    Jan 2012

    Posted 04 Apr 2013 Link to this post

    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
  6. Dimitar Tachev
    Admin
    Dimitar Tachev avatar
    67 posts

    Posted 04 Apr 2013 Link to this post

    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 >>
  7. Miladin
    Miladin avatar
    3 posts
    Member since:
    Jan 2012

    Posted 04 Apr 2013 Link to this post

    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
  8. Dimitar Tachev
    Admin
    Dimitar Tachev avatar
    67 posts

    Posted 09 Apr 2013 Link to this post

    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 >>
Back to Top
DevCraft banner