Data Access has been discontinued. Please refer to this page for more information.

How to: Obtain A Context Instance Without Passing A Connection String

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 can instantiate a context without the need of a connection string. The functionality is applicable only in the cases when at least one instance of the context was already created with a given connection string. It relays on the customized value of the OpenAccessContextBase.CacheKey property and this article demonstrates how to set up your model in order to use it.

  1. Add a new class to the project that holds the model called <context_name>.partial.cs (<context_name>.partial.vb). For example: EntitiesModel.partial.cs (EntitiesModels.partial.vb).
  2. Make sure that the name of the newly created class, and its definition, and its namespace match exactly those of the generated OpenAccessContext class.
  3. In the newly created class, override the CacheKey property so that it returns a value of your own choice (you can also apply some custom computed logic).

    using Telerik.OpenAccess;
    namespace MultipleModels
    {
        public partial class EntitiesModel
        {
            protected override string CacheKey
            {
                get
                {
                    return "my cache key";
                }
            }
        }
    }
    
    Imports Telerik.OpenAccess
    Namespace MultipleModels
        Partial Public Class EntitiesModel
            Protected Overrides ReadOnly Property CacheKey() As String
                Get
                    Return "my cache key"
                End Get
            End Property
        End Class
    End Namespace
    
  4. Save the file.

  5. Test the set up - at this point, you can instantiate the context for the first time by using one of the Telerik Data Access generated constructors and for the next times by passing String.Empty to the EntitiesModel(string connection) constructor. For example:

    string connectionString = @"SofiaCarRentalConnection";
        using (EntitiesModel dbContext = new EntitiesModel(connectionStringName))
        {
            //TODO: Execute Context Logic
        }
        using (EntitiesModel dbContext = new EntitiesModel(String.Empty))
        {
            //TODO: Execute Additional Context Logic
        }
    
    Dim connectionString As String = "SofiaCarRentalConnection"
        Using dbContext As New EntitiesModel(connectionStringName)
            'TODO: Execute Context Logic
        End Using
        Using dbContext As New EntitiesModel(String.Empty)
            'TODO: Execute Additional Context Logic
        End Using