How to: Obtain A Context Instance Without Passing A Connection String
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.
- Open the class file that holds your context 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 FluentModel : OpenAccessContext { protected override string CacheKey { get { return "my cache key"; } } } }
Imports Telerik.OpenAccess Namespace MultipleModels Partial Public Class FluentModel Inherits OpenAccessContext Protected Overrides ReadOnly Property CacheKey() As String Get Return "my cache key" End Get End Property End Class End Namespace
Save the file.
-
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 theFluentModel(string connection)
constructor. For example:string connectionString = @"SofiaCarRentalConnection"; using (FluentModel dbContext = new FluentModel(connectionStringName)) { //TODO: Execute Context Logic } using (FluentModel dbContext = new FluentModel(String.Empty)) { //TODO: Execute Additional Context Logic }
Dim connectionString As String = "SofiaCarRentalConnection" Using dbContext As New FluentModel(connectionStringName) 'TODO: Execute Context Logic End Using Using dbContext As New FluentModel(String.Empty) 'TODO: Execute Additional Context Logic End Using