I have been trying to re-create the tutorial ASP.Net Web API from the quick start scenario.
Is it just me or does the delete not work?
I receive a:
<
STRONG
><
EM
>Object references between two different object scopes are not
allowed. The object 'CarRentWebSite.Category' is already managed by
'ObjectScopeImpl 0x4' and was tried to be managed again by 'ObjectScopeImpl 0x3
OpenAccessRuntime.EnlistableObjectScope'.</
EM
></
STRONG
>
Kind regards,
Guy
5 Answers, 1 is accepted
In general this exception is thrown when a persistent object is managed by more than one context instances without detaching and reattaching the entity.
I tried to reproduce this behavior with the ASP.NET Web API quick start scenario but everything was working property - please find the project attached.
Could you please provide us with more details about the scenario where you are getting this issue and whether you have made any changes of the code from the Delete and GetBy methods of the OpenAccessBaseRepository which could be the reason for getting this exception?
I am looking forward to your feedback.
Dimitar Tachev
the Telerik team
Thanks for your reply.
After comparing the 2 projects, yours and mine, I noticed that there is a difference in OpenAccessBaseRepository.cs
Old version:
Public Overridable Function AddNew(entity As TEntity) As TEntity Implements IOpenAccessBaseRepository(Of TEntity, TContext).AddNew
If entity Is Nothing Then
Throw New ArgumentNullException("entity")
End If
Dim attachedEntity As TEntity = dataContext.AttachCopy(entity)
dataContext.SaveChanges()
Return attachedEntity
End Function
Public Overridable Function Update(entity As TEntity) As TEntity Implements IOpenAccessBaseRepository(Of TEntity, TContext).Update
If entity Is Nothing Then
Throw New ArgumentNullException("entity")
End If
Dim attachedEntity As TEntity = dataContext.AttachCopy(entity)
dataContext.SaveChanges()
Return attachedEntity
End Function
Public Overridable Sub Delete(entity As TEntity) Implements IOpenAccessBaseRepository(Of TEntity, TContext).Delete
If entity Is Nothing Then
Throw New ArgumentNullException("entity")
End If
dataContext.Delete(entity)
dataContext.SaveChanges()
End Sub
End Class
Your version:
Public Overridable Function AddNew(entity As TEntity) As TEntity Implements IOpenAccessBaseRepository(Of TEntity, TContext).AddNew
If entity Is Nothing Then
Throw New ArgumentNullException("entity")
End If
Dim attachedEntity As TEntity = dataContext.AttachCopy(entity)
dataContext.SaveChanges()
Dim detachedEntity As TEntity = dataContext.CreateDetachedCopy(attachedEntity, fetchStrategy)
Return detachedEntity
End Function
Public Overridable Function Update(entity As TEntity) As TEntity Implements IOpenAccessBaseRepository(Of TEntity, TContext).Update
If entity Is Nothing Then
Throw New ArgumentNullException("entity")
End If
Dim attachedEntity As TEntity = dataContext.AttachCopy(entity)
dataContext.SaveChanges()
Dim detachedEntity As TEntity = dataContext.CreateDetachedCopy(attachedEntity, fetchStrategy)
Return detachedEntity
End Function
Public Overridable Sub Delete(entity As TEntity) Implements IOpenAccessBaseRepository(Of TEntity, TContext).Delete
If entity Is Nothing Then
Throw New ArgumentNullException("entity")
End If
Dim attachedEntity As TEntity = dataContext.AttachCopy(entity)
dataContext.Delete(attachedEntity)
dataContext.SaveChanges()
End Sub
Kind regards,
Guy
Thank you for the provided details.
The difference between the versions is caused by an issue in the Delete method of the Web API Service code generation which is already fixed and you could find it updated in the latest internal build of Telerik OpenAccess ORM.
You could download it from: Your Account page -> Manage products menu -> Latest internal builds -> product: OpenAccess ORM version: 2013.1.219 -> Telerik-OpenAccess-ORM-2013.1.404.2.msi.
Dimitar Tachev
the Telerik team
I will try to follow these guidelines now to create a web api on our database.
Do I need to set some specific parameters in SQL Server 2008 R2 to use a web api?
Kind regards,
Guy
There aren't any specific settings of the SQL Server for generating the Web API services. You could just follow the Add OpenAccess Service Wizard which will set all of the necessary configurations for you.
Do not hesitate to contact us back if you need any further assistance.
Dimitar Tachev
the Telerik team