Article information
Article relates to
Telerik OpenAccess ORM Q3 2012 (version 2012.3.1012)
Created by
Doroteya Agayna
Last modified
October 30, 2012
Last modified by
NOTE: This knowledge base article solves a problem that occurs only in Telerik OpenAccess ORM Q3 2012, version 2012.3.1012 DESCRIPTION ASP.NET Web API is the newest framework of Microsoft for building HTTP services exposed to a wide range of clients and since Q3 2012, Telerik OpenAccess ORM supports it in the Add OpenAccess Service wizard.
Add OpenAccess Service saves developers a lot of time by automatically generating C#\Visual Basic code and necessary project files for taking advantage of OpenAccess with a wide variety of services. To do that, the wizard uses several templates, deployed on your machine by the installation of OpenAccess ORM: - WebApiBaseApiController.tt - WebApiBaseRepository.tt - WebApiController.tt - WebApiRepositories.tt
In Q3 2012 only, the Delete operation of the generated Web API services throws InvalidOperationException Message = Object references between two different object scopes are not allowed. The object 'InvalidOperationExceptionKB.Category' is already managed by 'ObjectScopeImpl 0xa' and was tried to be managed again by 'ObjectScopeImpl 0x9 OpenAccessRuntime.EnlistableObjectScope' In order to solve the problem, you can choose from the following options: - Replace two of the T4 code generation templates with the files, attached to this article - Create a custom template for your current project by modifying the deployed template SOLUTION 1 - Download the attached WebApiBaseRepository.zip - Extract it on your hard drive - Copy <extract_dir>\<language>\WebApiBaseRepository.tt to <install_dir>\dsl20XX\Extensibility\Dsw\Templates\<language> - Update all the Web API services that are already created by re-generating them and build the relevant assemblies From now on all the Web API services you create will no longer experience this issue when you delete an object. SOLUTION 2
- Create a new folder named OpenAccessTemplates in the host project. The Add OpenAccess Service Wizard is looking for code generation templates from the host project first and then from the OpenAccess installation folder - Copy <install_dir>\dsl20XX\Extensibility\Dsw\Templates\<language>\WebApiBaseRepository.tt to the OpenAccessTemplates folder - Add the template in your project - Delete the automatically generated code file. - Click on the template and press F4 to view its properties - Set Build Action to None and delete the text from the Custom Tool property - Open the template for edit - Find the following code: C#
public
virtual
void
Delete(TEntity entity)
{
if
(entity ==
null
)
throw
new
ArgumentNullException(
"entity"
);
dataContext.Delete(entity);
dataContext.SaveChanges();
}
Visual Basic
Public
Overridable
Sub
Delete(entity
As
TEntity)
Implements
IOpenAccessBaseRepository(Of TEntity, TContext).Delete
If
entity
Is
Nothing
Then
Throw
New
End
dataContext.Delete(entity)
dataContext.SaveChanges()
TEntity attachedEntity = dataContext.AttachCopy(entity);
dataContext.Delete(attachedEntity);
Dim
attachedEntity
TEntity = dataContext.AttachCopy(entity)
dataContext.Delete(attachedEntity)
Resources Buy Try