How to: Define an OpenAccessContext in a Separate Assembly
This article is relevant to entity models that utilize the deprecated Visual Studio integration of Telerik Data Access. The current documentation of the Data Access framework is available here.
Telerik Data Access provides you a Visual Studio Item Template that allows you to create a new OpenAccessContext based on an existing metadata source. The OpenAccessContext represents the container defined in the conceptual model. It contains a connection to the underlying data source and provides services such as change tracking and identity resolution. An OpenAccessContext is represented by an instance of the OpenAccessContext class.
When to Use the Telerik Data Access Domain Context Item Template?
The Telerik Data Access Domain Context item template is useful when you want to separate your classes and the context in different projects (assemblies).
Defining an OpenAccessContext in a Separate Assembly
Recall that the New Domain Model Wizard does not only create entity classes, but also creates an OpenAccessContext. There are two ways to generate a domain model with persistent classes only (without a context class):
- During the process of model generation.
- After the model is generated (by using the Visual Designer).
During the Process of Model Generation
The Advanced Options page from the New Domain Model Wizard exposes an option allowing you to generated only persistent classes (without a context class). This is the Generate context setting.
Set it to false. Thus, when you click Finish, the generated model will contain only persistent classes.
After the Model Is Generated
Once the model has been generated, you could remove the generated class by using the Visual Designer:
- Right-click on an empty are in the Visual Designer and select Show Model Settings.
- Go to the Advanced options tab page.
- Uncheck the Generate context setting.
- Click OK and save your model.
Generating the Context in a Separate Assembly.
To generate the context in a separate assembly:
- In the project that will contain the OpenAccessContext, add a reference to the project that contains the domain model.
-
In the Add New Item dialog, select the Telerik Data Access Domain Context item template. Give the context a name and click Add.
-
This will run the Domain Context Wizard.
The wizard contains only one page, allowing you to configure the following settings:
- Domain context name - this is the name of the generated class.
- Available metadata source - a drop-down showing all metadata sources included in your project.
- Entities grid - specifies which types will be exposed by the context. The generated context will contain read-only properties that return an IQueryable of each type that you want to work with.
When you click Finish, the wizard will generate and add to your project a new OpenAccessContext class.