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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:59:45+00:00 2026-06-17T16:59:45+00:00

I have a project that has 4 entity data model.for building them I don’t

  • 0

I have a project that has 4 entity data model.for building them I don’t want save a connection string in my project and I want store a connection string in app.config files and share it between my models.How I can do this?

thanks

  • 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-06-17T16:59:46+00:00Added an answer on June 17, 2026 at 4:59 pm

    Assuming DbContext and model/database first.

    1. Leave the generated EF connection strings in your app.config file intact. As Arthur said, they contain the paths to the EF metadata (csdl/ssdl/msl). They are also used during development by the model designer.
    2. Add a store connection string called e.g. “SharedConnection”. This is the only connection string which needs to be modified in production.
    3. Create a base class which derives from DbContext and derive all your contexts from that class.
    4. Create the default EF connection explicitely in the base class. Then modify it to use the shared connection string like that:
    public class BaseContext : DbContext
    {
        public BaseContext(string nameOrConnectionString)
            : base(CreateConnection(nameOrConnectionString), true)
        {
        }
    
        private static EntityConnection CreateConnection(string connectionString)
        {
            // Create a (plain old database) connection using the shared connection string.
            DbConnection dbConn = Database.DefaultConnectionFactory.CreateConnection(
                ConfigurationManager.ConnectionStrings["SharedConnection"].ConnectionString);
    
            // Create a helper EntityConnection object to build a MetadataWorkspace out of the
            // csdl/ssdl/msl parts of the generated EF connection string for this DbContext.
            EntityConnection wsBuilder = new EntityConnection(connectionString);
    
            // Merge the specific MetadataWorkspace and the shared DbConnection into a new EntityConnection.
            return new EntityConnection(wsBuilder.GetMetadataWorkspace(), dbConn);
        }
    }
    

    The code of the derived contexts does not change except that they must inherit from BaseContext. Here is a more robust CreateConnection method. It has error handling and removes the name of the shared connection string from the code at the price of adding an application setting.

    private static EntityConnection CreateConnection(string connectionString)
    {
        // Find the name of the shared connection string.
        const string appSettingKey = "SharedConnectionStringName";
    
        string sharedConnectionStringName = ConfigurationManager.AppSettings[appSettingKey];
        if (string.IsNullOrEmpty(sharedConnectionStringName))
        {
            throw new Exception(string.Format(
                "Shared connection not configured. " +
                "Please add a setting called \"{0}\" to the \"appSettings\" " +
                "section of the configuration file.", appSettingKey));
        }
    
        // Create a (plain old database) connection using the shared connection string.
        ConnectionStringSettings backendSettings =
            ConfigurationManager.ConnectionStrings[sharedConnectionStringName];
        if (backendSettings == null)
        {
            throw new Exception(string.Format(
                "Invalid connection string name \"{0}\" in appSetting \"{1}\"",
                sharedConnectionStringName, appSettingKey));
        }
    
        System.Data.Common.DbConnection dbConn =
            Database.DefaultConnectionFactory.CreateConnection(
            backendSettings.ConnectionString);
    
        // Create a helper EntityConnection object to build a MetadataWorkspace out of the
        // csdl/ssdl/msl parts of the generated EF connection string for this DbContext.
        EntityConnection wsBuilder = new EntityConnection(connectionString);
    
        // Merge the specific MetadataWorkspace and the shared DbConnection into a new EntityConnection.
        return new EntityConnection(wsBuilder.GetMetadataWorkspace(), dbConn);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have one project which has RIAService with entity framework that is referenced to
I have a core data model with an entity called clients that is made
I have a DAL project, with an entity data model context bound with EF
I'm trying to use Dynamic Data with a entity model that has Abstract class
I have a project that has several build configurations (FREE version, male-only, female-only, etc.).
I have a project that has a GUI (written in QT) and a command-line
I have a project that has components in several different directories and would like
We have a project that has been built in Flash and as3. It is
My use case is this... I have a project that has two production branches.
I have a client project that has posts assigned to their country of origin.

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.