Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7520255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:02:22+00:00 2026-05-30T02:02:22+00:00

I am using Linq2Sql in VS 2010 (C# .Net 4.0) to handle my database

  • 0

I am using Linq2Sql in VS 2010 (C# .Net 4.0) to handle my database access. This all works great.

But because my DataContext is in a separate layer to my web application (the data access layer) I have configured it to use the connection string in the calling web applications web.config file.

The end result requires one change to be made to the Linq2Sql designer file (DB.designer.cs) and that is to override the first constructor argument (which by default in a string for the DB connection string) and replace it with a call like follows:

System.Configuration.ConfigurationManager.ConnectionStrings["CS"].ConnectionString

The problem I currently seem to be having is that when I update the designer via the GUI designer (the drag and drop part) it resets this code change back to a hard coded connection string.

Can somebody please tell me if I can change a setting to prevent this? Or do I do something with partial classes? or maybe another way entirely?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T02:02:23+00:00Added an answer on May 30, 2026 at 2:02 am

    This is where partial methods come in handy! Thankfully, the LINQ2SQL DataContext is loaded with several very useful ones.
    Say your DataContext is named AutosDataContext as in my example. Create a new class alongside your DBML and call it AutosDataContext. Then fill it in as follows:

    public partial class AutosDataContext
        {
            partial void OnCreated()
            {
                this.Connection.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["CS"].ConnectionString;
            }
        }
    

    The partial OnCreated method gets called right after the DataContext is instantiated, and makes the perfect place for you to add your custom connection string logic without the DBML designer walking on it on update.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Been succcesfully adding records using Linq2Sql with the linq2sql genereated classes, works great.. But
I have recently inherited an ASP.Net app using Linq2SQL. Currently, it has its DataContext
Relevants: Visual Studio 2010, WCF WebServices, Linq2Sql, .Net 4.0 Forgive me if this is
I'm using linq2sql against an old database system. In this system there are three
I'm using C# , .NET and SQL Server with Linq2SQL. What does this error
When using Linq2sql everything automagically works. My experience is that going with the flow
I'm using Linq2Sql for my asp.net mvc project and so far it has worked
I'm using Linq2Sql with pipes/filters. My database consists of Accounts and Transactions tables where
what is the preferred practice when linq2sql using (in asp.net mvc applications): to create
We are using C# and Linq2SQL to get data from a database for some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.