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

Object references between two different object scopes are not allowed

3 Answers 432 Views
Data Access Free Edition
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Mike
Top achievements
Rank 1
Mike asked on 04 May 2015, 12:56 AM

I have an interesting issue i can not solve at present. I am new to OA and finding it a nice product to use.

 My issue is i have a windows forms app, the form uses a bindingsource control, when the form loads the form is bound to a new instance of the entity

 source.DataSource=new Address();

At this point the address has not been added to the unit of work so all " should " be ok. Then if i select an address from a drop down and set the binding source 

source.DataSource=(Address)lookup.GetSelectedDataRow(); // gets the selected data row from a dropdown

now making changes to the form is fine and then when i call save changes i get the below exception

 Now the initial instance of the datasource was not added into the unit of work so i did not expect it to be managed UNTILL i called unitofwork.Add().

How can avoid this occurring or how can i remove the " initial value " from the ObjectScope?? I even tried creating a detached entity thinking that the scope would not be tracking the entity (even though it was not added into the unit of work) but this did not work as it was a " new " instance and was not tracked.

Is there a way to remove an entity from the object graph?? Or not have the entity managed until it is added to the unit of work.

Cheers and thankyou in advance.

Mike.

 

Telerik.OpenAccess.Exceptions.InvalidOperationException: Object references between two different object scopes are not allowed. The object 'FreightMaster.Domain.Address' is already managed by 'ObjectScopeImpl 0x3' and was tried to be managed again by 'ObjectScopeImpl 0x6 OpenAccessRuntime.EnlistableObjectScope'.
   at Telerik.OpenAccess.SPI.Backends.ThrowException(Exception e)
   at OpenAccessRuntime.ExceptionWrapper.Throw()
   at OpenAccessRuntime.DataObjects.OpenAccessPersistenceManagerImp.handleException(Exception x, Boolean needsRollback)
   at OpenAccessRuntime.DataObjects.OpenAccessPersistenceManagerImp.makePersistent(Object o)
   at OpenAccessRuntime.DataObjects.UnsynchronizedPMProxy.makePersistent(Object o)
   at OpenAccessRuntime.ObjectScope.Add(Object pc)
   at Telerik.OpenAccess.OpenAccessContextBase.Add(Object entity)
   at FreightMaster.UnitOfWork.Context.AddEntity(Object entity) in c:\Projects\FreightMaster\FreightMaster.Data\DataContext\Context.cs:line 245
   at FreightMaster.UI.Freight.ConsignmentNote.SaveChanges() in c:\Projects\FreightMaster\FreightMaster.UI.Freight\Forms\ConsignmentNote.cs:line 207

3 Answers, 1 is accepted

Sort by
0
Ralph Waldenmaier
Telerik team
answered on 05 May 2015, 07:36 AM
Hello Mike,
Thank you for contacting us.
What is actually your lookup variable that you use to for GetSelectedDataRow(). If it is a context instance, then the resulting object is managed by this context. If you now try to add this to another context, you will get the reported exception, since an object can not be managed by 2 different context instances.
To get more details about binding and working with windows forms apps, I would like to recommend to you our documentation about this topic. You can find it here http://docs.telerik.com/data-access/quick-start-scenarios/winforms/getting-started-root-quickstart-winforms-overview

Additionally, you might find our SDK examples interesting covering different aspects of the product. You can download it here. http://www.telerik.com/data-access/samples-kit

I hope this information is helpful for you.
Do come back in case you need further assistance.

Regards,
Ralph Waldenmaier
Telerik
 
OpenAccess ORM is now Telerik Data Access. For more information on the new names, please, check out the Telerik Product Map.
 
0
Mike
Top achievements
Rank 1
answered on 14 May 2015, 06:40 AM

Ralph,

 Thankyou. i was able to get the entity to save. I have another issue though, deleting an entity. If the entity has been saved then i do not have an issue, it deletes just fine, however if i add a record to a grid then remove it (and not save) i get " instance is transient " exception. I have looked at the model and the entity is not a dependant and is managed.

 

I am creating the entity like this

CItem cItem=new CItem();

ParentItems.ChildItems.Add(cItem);

then if i call context.Delete(cItem) i get the exception.

My question is, how can a non-persisted entity be removed from the context.

 

Cheers

0
Ralph Waldenmaier
Telerik team
answered on 15 May 2015, 07:35 AM
Hi Mike,
It seems that either ParentItems or cItem is not yet bound to a context. In this case Data Access does not know anything about that object and can therefore not maintain it.
You can either call context.Add(cItem) or setting the IsManaged property of the ChildItems collection to true. This should now add the object to the context internally for you.
You might find this link interesting which covers this.

Hope this helps.
Feel free to ask in case you have any other question.

Regards,
Ralph Waldenmaier
Telerik
 
OpenAccess ORM is now Telerik Data Access. For more information on the new names, please, check out the Telerik Product Map.
 
Tags
Data Access Free Edition
Asked by
Mike
Top achievements
Rank 1
Answers by
Ralph Waldenmaier
Telerik team
Mike
Top achievements
Rank 1
Share this question
or