Exception:AttachCopy cannot attach yet instances that are not clean or modified

6 posts, 0 answers
  1. john
    john avatar
    67 posts
    Member since:
    Jan 2012

    Posted 17 Sep 2012 Link to this post

    When I process following code, it will throw exception:AttachCopy cannot attach yet instances that are not clean or modified 
    context.AttachCopy<Function>(entity);         

    The scenario is that I edit a record in RadGrid and don't modify value and submit save button to call above code. My question is that whether I have a way to check whether the object is modified or how can I avoid the exception? At least, I need a way to notice user the information instead of throw exception.
  2. john
    john avatar
    67 posts
    Member since:
    Jan 2012

    Posted 19 Sep 2012 Link to this post

    Any solution?
  3. DevCraft banner
  4. PetarP
    Admin
    PetarP avatar
    754 posts

    Posted 19 Sep 2012 Link to this post

    Hi john,

     You can get the state of your persistent object using the Persistent State API we provide.
    Here is an example:

    ObjectState state = OpenAccessContext.PersistenceState.GetState(car);
    Basically right now you can only attach objects that are either Clean, Dirty or New.

    All the best,
    Petar
    the Telerik team
    Follow @OpenAccessORM Twitter channel to be the first one to get the latest updates on new releases, tips and tricks and sneak peeks at our product labs!
  5. Aaron
    Aaron avatar
    9 posts
    Member since:
    Oct 2012

    Posted 04 Dec 2013 Link to this post

    Can you please explain to me why the following code doesn't work?

    (ObjectState state = AppRegistryDataContext.PersistenceState.GetState(Model);
     
                    if (state.HasFlag(ObjectState.Clean) ||
                        state.HasFlag(ObjectState.Dirty) ||
                        state.HasFlag(ObjectState.New))
                    {
                        //attach the object in order to persist the changes.
                        db.AttachCopy(Model);
                    }


    Upon debugging, GetState returns a value of 60.  But, based on the values of the ObjectState enum, Clean and Dirty both return true so the AttachCopy gets called and returns the error referenced in the start of this thread.

    Were these combination values set that way on purpose or was it a mistake?  I ask because the values appear to be correct up until the value for NotLoaded (48), but then they go into combination values like Clean which is 56 (111000 in binary) which means that if Clean is set, so are NotLoaded (48), MaskNoMask (32), MaskManaged (16), and MaskLoaded(8).  In my case, the value is 60 so that means that MaskNoMask, MaskManaged, MaskLoaded, and MaskDirty are all set.  The problem is, of course, that Clean and Dirty also return true on a bitwise check because their values line up in binary because these are incorrect Flags values unless it was intended that way and if it was intended that way, then there are other issues with your OpenAccess code.
  6. Thomas
    Admin
    Thomas avatar
    590 posts

    Posted 06 Dec 2013 Link to this post

    Hello,

    the bit values represent partly masks, and partly states the instances have. If you are looking for modified objects, you should use the MaskDirty, MaskNew and MaskDeleted flags.

    if (state.HasFlag(ObjectState.MaskLoaded) && // includes the clean
        state.HasFlag(ObjectState.MaskDirty)) // excludes the clean, but leaves the new,deleted,modified
    {
       action
    }

    This is a bit (!) difficult to understand, but the values are really coming from a bit mask, and that's why the enum is declared with the [Flags] attribute.

    Regards,
    Thomas
    Telerik
    OpenAccess ORM Q3 2013 simplifies your model operations even further providing you with greater flexibility. Check out the list of new features shipped with our latest release!
  7. Aaron
    Aaron avatar
    9 posts
    Member since:
    Oct 2012

    Posted 06 Dec 2013 Link to this post

    Thomas, thanks for the reply.  I understand bit masks, I just wasn't sure if the partly mask was what was intended or not.  Btw, I laughed at that little bit (!) of a pun you made.  Anyways, I found a way to solve my issue as described in this thread that you also replied to.  I still have some questions, but they are more related to the other thread so I will post there.  Thanks again!
Back to Top
DevCraft banner