Andrea Azzola

Tips and Techniques for Lifestyle Design

Set the ConnectionString at Runtime with DynamicData

Posted on [Permalink]

The following snippet customizes the .NET ConnectionString property at runtime in a Dynamic Data application.

Edit the file Global.asax and modify the RegisterRoutes(RouteCollection routes) method. Instead of the following statement:

model.RegisterContext(typeof(XYZDataContext), new ContextConfiguration() { ScaffoldAllTables = true });

Use

model.RegisterContext
(
    () => new XYZDataContext(connectionString),
    new ContextConfiguration() { ScaffoldAllTables = true }
);

Where connectionString is your string value. The RegisterRoutes's parameter collection can be customized without comprimosing the application functionality.

Categories: