Using LINQ to SQL and ConnectionStrings in a Class Library

I was looking to separate my LINQ to SQL data layer from the rest of my application. So, I decided to move the files to a class library. This way, when my application builds, all of my LINQ files are found in MyApp.DL or something similar. However, I wanted the class library to use the SQL connection that I have set in my web.config. Because the class library does not have a reference to System.Configuration, I had to add that first. I then created a partial class for my Context object. Inside, I created a new constructor with a boolean parameter called UseWebConfigConnectionString. My web.config would contain a generic connection string name called LinqSqlConnectionString.This allows me to make changes to the LINQ files, save them, and not affect this method. So, here's what I ended up with:

web.config

<connectionStrings>
    <clear/>
    <add name="LinqSqlConnectionString" connectionString="{insert settings here};" providerName="System.Data.SqlClient"/>
</connectionStrings>

MyApp.DL.ExtendedLinqObjects.vb

Imports System.Configuration

Partial Public Class MyDataContext
    Public Sub New(ByVal UseWebConfigConnectionString As Boolean)
        MyBase.New(ConfigurationManager.ConnectionStrings("LinqSqlConnectionString").ConnectionString, mappingSource)
        OnCreated()
    End Sub
End Class

sample.aspx.vb

Dim db As New MyDataContext(True)

This seemed to be the easiest way to pull from my connection strings setting. Although, I'd highly recommend encrypting your connection string before deploying to production.

Published Tuesday, May 20, 2008 2:22 PM by Jason N. Gaylord

Comments

# re: Using LINQ to SQL and ConnectionStrings in a Class Library

Tuesday, May 20, 2008 5:44 PM by Kevin

What happens when you pass in false to the constructor....

# re: Using LINQ to SQL and ConnectionStrings in a Class Library

Tuesday, May 20, 2008 11:23 PM by n.ocampo

I don't see any use of the parameter on the constructor.

# re: Using LINQ to SQL and ConnectionStrings in a Class Library

Wednesday, May 21, 2008 11:40 AM by Jason N. Gaylord

You're right. There is no use of the parameter on the constructor (other than a reminder of the purpose for this constructor). However, the New() constructor is auto-generated from the LINQ designer. So, if you make changes to your LINQ to SQL file using the VS desgin tools, the New() constructor will be rewritten. I've simply added another partial class to use the constructor to pull from the web.config file rather than the app.config file. It's not the cleanest solution, but it works.

# re: Using LINQ to SQL and ConnectionStrings in a Class Library

Wednesday, May 21, 2008 12:04 PM by Martillo

The LINQ datacontext connection string issue is definitely a problem, especially if you use the O/R Designer.

Here's a post that describes what may be a slightly cleaner solution:

ddkonline.blogspot.com/.../set-connection-string-in-linq-dbml-file.html

If you use the method described in that post, in the Data section of the DMBL file properties you need to set the Connection property to "None" and the Application Settings property to "False" (this will prevent the designer from regenerating a default constructor). However, you'll need to reset the Connection property back to "None" if you drag and drop any new items from the database onto the design surface. So maybe it's not that clean after all. Who knows...

I took it a little further and instead of calling ConfigurationManager.ConnectionStrings I call a property in a Utility library that returns the correct connection string depending on the current environment (development, staging or production).

# re: Using LINQ to SQL and ConnectionStrings in a Class Library

Wednesday, May 21, 2008 12:56 PM by Jason N. Gaylord

That's not bad either. Good suggestion.

# re: Using LINQ to SQL and ConnectionStrings in a Class Library

Friday, June 27, 2008 2:32 PM by Paul Brown

Great idea. Lead me to a slightly different <a href="pretzelsteelersfan.blogspot.com/.../use-webconfig-connection-string-in-your.html"> take</a>.

Leave a Comment

(required) 
(required) 
(optional)
(required)