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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T03:45:42+00:00 2026-05-18T03:45:42+00:00

I am new to EF using EF4 with Database First and generation taking place.

  • 0

I am new to EF using EF4 with Database First and generation taking place.
I have to place the connection string in a different config than the app.config.

How can I do it? How can I bypass it?

I have a partial class of MyTextContext
and I have a method like this

   public static string GenerateConnectionString()
  {
     SqlConnectionStringBuilder sqlBuilder = new SqlConnectionStringBuilder();

     // Set the properties for the data source.
     sqlBuilder.DataSource = dbServer;
     sqlBuilder.InitialCatalog = dbName;
     sqlBuilder.UserID = "YOUR_USERNAME";
     sqlBuilder.Password = "YOUR_PASSWORD";
     sqlBuilder.IntegratedSecurity = false;

     // Build the SqlConnection connection string.
     string providerString = sqlBuilder.ToString();

     // Initialize the EntityConnectionStringBuilder.
     var entityBuilder = new EntityConnectionStringBuilder();

     //Set the provider name.
     entityBuilder.Provider = "System.Data.SqlClient";

     // Set the provider-specific connection string.
     entityBuilder.ProviderConnectionString = providerString;

     // Set the Metadata location.
     entityBuilder.Metadata = @"res://*/myTestModel.csdl|
                            res://*/myTestModel.ssdl|
                            res://*/myTestModel.msl";


     return entityBuilder.ToString();
  }

I have noticed that my EFModel.designer has a constructor like these:

    /// <summary>
    /// Initializes a new MyTestContext object using the connection string found in the 'MyTestContext' section of the application configuration file.
    /// </summary>
    public MyTestContext() : base("name=MyTestContext", "MyTestContext")
    {
        this.ContextOptions.LazyLoadingEnabled = true;
        OnContextCreated();
    }

    /// <summary>
    /// Initialize a new MyTestContext object.
    /// </summary>
    public MyTestContext(string connectionString) : base(connectionString, "MyTestContext")
    {
        this.ContextOptions.LazyLoadingEnabled = true;
        OnContextCreated();
    }

    /// <summary>
    /// Initialize a new MyTestContext object.
    /// </summary>
    public MyTestContext(EntityConnection connection) : base(connection, "MyTestContext")
    {
        this.ContextOptions.LazyLoadingEnabled = true;
        OnContextCreated();
    }

How can I use my “GenerateConnectionString” rather then EF reading from the app.config?

Thanks for any suggestions

  • 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-18T03:45:42+00:00Added an answer on May 18, 2026 at 3:45 am

    Did you try using the 2nd ctor overload?

    public MyTestContext(string connectionString)
    

    e.g

    var ctx = new MyTestContext(GenerateConnectionString());
    

    From MSDN:

    The Entity Data Model tools generate a named connection string that is stored in the application’s configuration file. This named connection string can be supplied instead of the connectionString parameter when instantiating the ObjectContext class.

    So in theory, it should use the one you supply (unless you use the parameterless constructor).

    I’ve never tried this though (i don’t use code generation, i hand craft everything).

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

Sidebar

Related Questions

I still very new using Subversion. Is it possible to have a working copy
I'm kinda new to using Rails, and an app I am working on is
I have a question about using new[] . Imagine this: Object.SomeProperty = new[] {string1,
I am using EF4 and MVC 2. I am inserting a new record to
Im new to MVC, EF4 and Linq, so forgive my ignorance If im using
I'm new to using the NetBeans IDE. When I try to look at the
I'm new to using LINQ to Entities (or Entity Framework whatever they're calling it)
I am fairly new to using infragistics controls (started yesterday). While they are (very)
I am relatively new to using jQuery and would like to use the load
I'm new to using open source libraries in production code so when it says

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.