Consuming a Model - Configuration
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.
When you use the Telerik Data Access Class Library template in Visual Studio, a new Class Library project will be added to the solution, and the Telerik Data Access New Domain Model Wizard will run. Once the domain model is configured, a new RLINQ file will be added to the project. By following the steps described in the Database First Scenario or Model First Scenario topics, you will end up creating a new Telerik Data Access Domain Model and capsulating it in a separate project (assembly). With the model in its own project, you are able to re-use it in different applications. It is a best practice to create your Telerik Data Access Domain Model in its own project and there are few steps you need to be aware of when you want to use the domain model from another project. Most of the topics in the documentation follow this pattern. So rather than walk these steps each time, you can learn them here.
Follow the steps in the Database First Scenario topic and create a new Telerik Data Access Class Library project. For the sake of this topic, base your domain model on the SofiaCarRental sample database (or you could use your own database).
The OpenAccessModel project contains two items. The first one is the domain model, i.e. the rlinq file. The second file is a configuration file containing the connection string to the target database.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings>
<add name="SofiaCarRental21Connection"
connectionString="data source=(local);initial catalog=SofiaCarRental21;
integrated security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
There are few steps you need to perform, in order to ensure that the domain model can be used from another project. To illustrate this idea, add a new Console Application to your solution. Note that this pattern could be applied for all other types of projects, such as WinFroms, WPF, ASP.NET, etc.
In order to use the model in the client project (in this case, this is the ConsumerProject), there are three steps you need to perform:
-
In the client project, add a reference to the Telerik Data Access Class Library project, i.e. the OpenAccessModel project. Thus, you will have an access to the domain model classes.
-
In the client project, add references to the Telerik.OpenAccess.dll and Telerik.OpenAccess.35.Extensions.dll assemblies. The consuming project will work with Telerik Data Access, so you need to add references to these assemblies.
-
The last step is to ensure that the client project "has" the ability to know about the domain model, i.e. to have an access to the database connection string used by the rlinq file. What you normally do when you have an existing config file in the client project is to copy the connection string from the App.config in the library project and paste it in the <connectionStrings/> section in the client config file.
If you don't have an existing config file in the client project, as in this example, you simply could copy and paste the entire config file.In a nutshell, the reason behind this step is that the ConsumerProject project is your executable project. When you run the application, all settings (configurations) are taken from that project. Respectively, the OpenAccessContext will try to retrieve the connection string from the config file in the executable project. If such doesn't exist, the initialization of the OpenAccessContext will fail.
So, these are the three important pieces you need in order to consume your domain model.
- Reference to the library project.
- References to Telerik.OpenAccess.dll and Telerik.OpenAccess.35.Extensions.dll.
- Copy and paste the connection string from the library project to the consuming project.
Additional step you need to consider is making the client (consuming) project a start up project. Right-click the client (consuming) project in Solution Explorer and select Set as Startup Project.
Exposing the Model Through WCF Services
For more information, please refer to the Web Services topic.
Next Steps
Once your client (consuming) project is configured to use the Telerik Data Access Domain Model, the next step is to start performing CRUD operations.