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 635381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:23:54+00:00 2026-05-13T20:23:54+00:00

In LinqToSql I could write an empty constructor if I set the Connection string

  • 0

In LinqToSql I could write an empty constructor if I set the Connection string to (None). In that empty constructor I could then pull the connection string from the web.Config like so:

    public CustomDataDataContext() :
  base( ConfigurationManager.ConnectionStrings[ "DB" ].ConnectionString, mappingSource )
{
  OnCreated();
}

I am trying to do the same thing now in Entity Framework 4.0. Is this possible somehow? I can’t seem to get rid of their generated paramaterless constructor. I would rather not wrap the context in a new class that feeds the context the connection string to the context if I do not have to.

  • 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-13T20:23:55+00:00Added an answer on May 13, 2026 at 8:23 pm

    While a parameterless constructor which automatically finds a connection seems like a good idea, it tightly couples the object context to the configuration file.

    It also introduces some “magic” in that the consumer has no clear way to determine how to change the connection string associated with the parameterless constructor (except to read the source).

    I guess my answer is, regardless of whether the Entity Framework allows you to do this, you probably shouldn’t. Use a factory instead, and achieve the same flexibility without tying the object context to the configuration system:

    public interface ICustomDataContextFactory
    {
        CustomDataContext Create();
    }
    
    public class CustomDataContextFactory : ICustomDataContextFactory
    {
        private readonly string _connectionStringName;
    
        public CustomDataContextFactory(string connectionStringName)
        {
            _connectionStringName = connectionStringName;
        }
    
        public CustomDataContext Create()
        {
            var connectionString = ConfigurationManager.ConnectionStrings[_connectionStringName].ConnectionString;
    
            return new CustomDataContext(connectionString);
        }
    }
    

    This provides all consumers the same mechanism for creating a parameterless instance, while allowing implementors to decide how the instance is created – perhaps a different connection string needs to be used for a particular instance, or the database name is read from the command line on startup.

    I realize that you said you don’t want to introduce a new class which passes the connection string through. I took that to mean a class derived from CustomDataContext and not a factory, which is a new concept here.

    (This answer only really applies to production code. The parameterless constructor trick is useful in ephemeral code like proofs-of-concept.)

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

Sidebar

Ask A Question

Stats

  • Questions 357k
  • Answers 357k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You probably want to call setlocale() first, "LC_ALL" should do… May 14, 2026 at 9:06 am
  • Editorial Team
    Editorial Team added an answer Linux Ubuntu Desktop Jaunty Firebug FireCookie Pixel Perfect Web developer… May 14, 2026 at 9:06 am
  • Editorial Team
    Editorial Team added an answer Your code should look like this: var par = [];… May 14, 2026 at 9:06 am

Related Questions

LINQToSQL doesn't like my sproc. It says that the sproc has return type none,
I'm trying to write some LINQ To SQL code that would generate SQL like
I am having a lot of fun with Linq2Sql. Expression Trees have been great,
I created a batch file to run SqlMetal and generate Linq2Sql data classes, check
Is there a better/shorter way to handle (lots of) null references, for example when

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.