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

  • Home
  • SEARCH
  • 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 4029646
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:22:18+00:00 2026-05-20T11:22:18+00:00

How can I retrieve the Entity Framework 4 connection string from a custom config

  • 0

How can I retrieve the Entity Framework 4 connection string from a custom config file, not web.config?

Edit:
Is it reasonable to delete the default constructor generated code and recreate it in a partial class to use the pulled in connection string?

I would really like to avoid changing all references to the EF context with an overloaded method including the connection string.

@BrokenGlass: This is what we ended up with:

public partial class STARSEntities
{
    private const string _connectionStringFormat = @"metadata=res://*/STARS.EntityModel.STARSModel.csdl|res://*/STARS.EntityModel.STARSModel.ssdl|res://*/STARS.EntityModel.STARSModel.msl;provider=System.Data.SqlClient;provider connection string='Data Source={0};MultipleActiveResultSets=True'";

    /// <summary>
    /// Initializes a new STARSEntities object using the connection string found in the STARS.xml configuration file.
    /// </summary>
    /// <remarks>
    /// If the STARSEntities class is regenerated from the database, the default constructor needs to be removed from the generated file.
    /// </remarks>
    public STARSEntities() : base(GetConnectionString(), "STARSEntities")
    {
        this.ContextOptions.LazyLoadingEnabled = true;
        OnContextCreated();
    }

    private static string GetConnectionString()
    {
        return string.Format(_connectionStringFormat, ApplicationConfiguration.GetConnectionString("STARS"));
    }

}
  • 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-20T11:22:19+00:00Added an answer on May 20, 2026 at 11:22 am

    There’s a constructor overload for DataContext that you can pass a connection string – in that case you can take the setting from anywhere you like.

    Edit based on updated question:

    I would really like to avoid changing
    all references to the EF context with
    an overloaded method including the
    connection string.

    The problem is that the Entities context created by the T4 script generates a const property that is used as connection string

        public const string ConnectionString = "name=FooEntities";
    
        public FooEntities()
            : base(ConnectionString, ContainerName)
        {
            this.ContextOptions.LazyLoadingEnabled = true;
        }
    

    Since you can’t override the default constructor of the partial class, your only other option would be to change the T4 script itself – you should see the following in your .TT script file:

        public <#=code.Escape(container)#>()
            : base(ConnectionString, ContainerName)
        {
    <#
            WriteLazyLoadingEnabled(container);
    #>
        }
    

    To force your connection string to be used you could modify the constructor call to determine the connection string by calling a static method that you define in a separate file (but for the same partial class FooEntities):

        public <#=code.Escape(container)#>()
            : base(GetCustomConnectionString(), ContainerName)
        {
    <#
            WriteLazyLoadingEnabled(container);
    #>
        }
    

    Now GetCustomConnectionString() can be defined separately

    public partial class FooEntities : ObjectContext
    {
       public static string GetCustomConnectionString()
      {
         return "Foobar"; //however you want to determine connection string here
      }
    }
    

    You see this is getting complicated and fragile very fast, so I would not advise doing this – but you could.

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

Sidebar

Related Questions

I have a WCF service that uses the entity framework to retrieve records from
In asp.net, you can retrieve MULTIPLE datatables from a single call to the database.
Can a Linq query retrieve BLOBs from a Sql Database? And how do they
How can I retrieve Tuples at Select using EF4? var productCount = (from product
How can I retrieve the page title of a webpage (title html tag) using
How can I retrieve the current working directory of cmd.exe? This seems possible. For
How can I retrieve the position of the current Para? <xsl:template match=Para> <xsl:variable name=PositionInDocument
How can I retrieve the information shown in the Mac's System Profiler app programatically?
I am looking to extend jQuery so I can easily retrieve the tagName of
Reading the bottom of the documentation, specifically: You can still retrieve properties of deleted

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.