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

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

5 Answers 76 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
john
Top achievements
Rank 1
john asked on 17 Sep 2012, 12:28 PM
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.

5 Answers, 1 is accepted

Sort by
0
john
Top achievements
Rank 1
answered on 19 Sep 2012, 02:22 PM
Any solution?
0
PetarP
Telerik team
answered on 19 Sep 2012, 03:02 PM
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!
0
Aaron
Top achievements
Rank 1
answered on 04 Dec 2013, 06:31 PM
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.
0
Thomas
Telerik team
answered on 06 Dec 2013, 07:51 AM
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!
0
Aaron
Top achievements
Rank 1
answered on 06 Dec 2013, 03:19 PM
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!
Tags
General Discussions
Asked by
john
Top achievements
Rank 1
Answers by
john
Top achievements
Rank 1
PetarP
Telerik team
Aaron
Top achievements
Rank 1
Thomas
Telerik team
Share this question
or