<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://msmvps.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>SuperSKa Weblog : LINQ</title><link>http://msmvps.com/blogs/superska/archive/tags/LINQ/default.aspx</link><description>Tags: LINQ</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP2 (Build: 40407.4157)</generator><item><title>LINQ to SQL Connectionstring in web.config</title><link>http://msmvps.com/blogs/superska/archive/2009/03/13/linq-to-sql-connectionstring-in-web-config.aspx</link><pubDate>Fri, 13 Mar 2009 04:38:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1677869</guid><dc:creator>Stefan Kamphuis</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/superska/rsscomments.aspx?PostID=1677869</wfw:commentRss><comments>http://msmvps.com/blogs/superska/archive/2009/03/13/linq-to-sql-connectionstring-in-web-config.aspx#comments</comments><description>&lt;p&gt;For starters, there&amp;#39;s probably dozens of places on the web already, with a solution for this very same issue, but here&amp;#39;s mine anyway.&lt;/p&gt;  &lt;p&gt;The issue is this: when creating a website in ASP.NET using LINQ to SQL, you&amp;#39;ve probably guessed it would be a good idea to put the connectionstring in web.config. Especially since there&amp;#39;s a special section for them there.&lt;/p&gt;  &lt;p&gt;However, when you create a LINQ to SQL Databaclasses (.dbml) file and drag your tables on it, it&amp;#39;s automatically configured to use the database you dragged the tables from.&lt;/p&gt;  &lt;p&gt;And it&amp;#39;s pretty easy to solve too. First, we want to get the connectionstring out of the .dbml file. To do this, open the .dbml file in the designer and change it&amp;#39;s data-properties to match this:&lt;/p&gt;  &lt;p&gt;&lt;img style="border-bottom:0px;border-left:0px;border-top:0px;border-right:0px;" border="0" alt="image" src="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/superska/image_5F00_3.png" width="419" height="125" /&gt; &lt;/p&gt;  &lt;p&gt;Then, create a new partial class for the generated DataContext class with only a default constructor in it. This default constructor will pass the connectionstring from web.config to the constructor if it&amp;#39;s base class, and you&amp;#39;re al set:&lt;/p&gt;  &lt;pre style="border-bottom:#cecece 1px solid;border-left:#cecece 1px solid;padding-bottom:5px;background-color:#fbfbfb;min-height:40px;padding-left:5px;width:450px;padding-right:5px;overflow:auto;border-top:#cecece 1px solid;border-right:#cecece 1px solid;padding-top:5px;"&gt;&lt;pre style="background-color:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:10px;"&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; partial &lt;span style="color:#0000ff;"&gt;class&lt;/span&gt; MyDataContext{
&lt;/pre&gt;&lt;pre style="background-color:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:10px;"&gt;    &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt; MyDataContext()
&lt;/pre&gt;&lt;pre style="background-color:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:10px;"&gt;        : &lt;span style="color:#0000ff;"&gt;base&lt;/span&gt;(ConfigurationManager.ConnectionStrings[&amp;quot;&lt;span style="color:#8b0000;"&gt;MyConnectionString&lt;/span&gt;&amp;quot;].ConnectionString,mappingSource)
&lt;/pre&gt;&lt;pre style="background-color:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:10px;"&gt;    {
&lt;/pre&gt;&lt;pre style="background-color:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:10px;"&gt;    }
&lt;/pre&gt;&lt;pre style="background-color:#fbfbfb;margin:0em;width:100%;font-family:consolas,&amp;#39;Courier New&amp;#39;,courier,monospace;font-size:10px;"&gt;}&lt;/pre&gt;&lt;/pre&gt;

&lt;p&gt;This way, you can always regenerate your dataclasses without having to remember to change &lt;/p&gt;

&lt;p&gt;use the right connectstring.&lt;/p&gt;

&lt;p&gt;Have Fun!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1677869" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/superska/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://msmvps.com/blogs/superska/archive/tags/LINQ/default.aspx">LINQ</category></item></channel></rss>