Web developers wiki ASP.NET Sitecore Sharepoint Kentico by Evident Interactive

Linq to Sql: Change connectionstring to load from Web.config

Modified: 2008/11/18 10:12 by Pieter Brinkman - Categorized as: ASP.NET, Linq


If you want to use your connectionstring from the web.config with Linq to Sql (dbml) you have to add the following partial class to your project:

C#:
partial class yourDataContext
{
 partial void OnCreated()
 {
  ConnectionStringSettings s = ConfigurationManager.ConnectionStrings["YourConnectionString"];
  if (s != null) 
   Connection.ConnectionString = s.ConnectionString;
 } 
}

Don't forget to change the yourDataContext and YourConnectionString to fit your project.

Source: blog.newguid.net

 © Evident Interactive BV