1

SHORT VERSION: Is it possible and how to define a variable in a web.config file that I can use within itself.

I am new to a company and also a beginner developer in C# MVC Web applications. I have been tasked to maintain and test a web application that has multiple connections strings in a single web.config file. These connection strings, however, have the same values in it like:

<connectionStrings>
    <add name="DefaultConnection" connectionString="provider=System.Data.SqlClient;provider connection string=&quot;data source=SUPERCOMPUTER\SQLEXPRESS2012;initial catalog=HumanResourceDB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
    <add name="DefaultConnection3" connectionString="Data Source=SUPERCOMPUTER\SQLEXPRESS2012;Initial Catalog=HumanResourceDB;Integrated Security=True" providerName="System.Data.SqlClient" />
  </connectionStrings>

As you can see, the difference with these two is just the Name of the connection. Since Im a beginner I don't exactly know why this application has to use 2 same connection strings with different names. I was told that one is being used by Database First entity framework and the other is being used by ASP.NET Identity Code First and I do not know right now what those are.

The problem: I am CONSTANTLY changing connection strings value to test with different machines/environment/database etc and the need to modify 2 connection strings is really slowing me down.

The question: Is it possible to use variables that I can just declare at the top of the web config file and use it in the connection strings so that I can just modify the variable values. Something like this:

var Server = SUPERCOMPUTER\SQLEXPRESS2012
var IntegratedSecurity = true
var database = HumanResourceDB
var user = databaseusername
var password = databasepassword

<connectionStrings>
    <add name="DefaultConnection" connectionString="... data source=**Server**;initial catalog=**database**;integrated security=**IntegratedSecurity **;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
    <add name="DefaultConnection3" connectionString="Data Source=**Server**;Initial Catalog=**database**;Integrated Security=**IntegratedSecurity**" providerName="System.Data.SqlClient" />
  </connectionStrings>

Additional Question: Is it really impossible to use one connection string if the mvc application relies on both Database First Entity Framework and Code First .NET Identity?

2
  • There are many things you could do. Easiest thing (since it is very googleable) might be to use config transforms. Commented Feb 15, 2017 at 11:53
  • What did your research show? You can use configuration transformations, or use different connection string names from code ("Default_Env_1", "Default_Env_2") where every different name points to a different database. Commented Feb 15, 2017 at 11:54

1 Answer 1

1

All EF project have a dbContext File..so you can use multiple EF projects(Code First, DB first) in just one solution.

Yes its clearly possible ...in code first your db context can get the name of each ConnectionString you want!.. like this:

    public class AppContext : DbContext
{
    public AppContext() : base("ConString Name")
    {

    }
}

so you can change the connection string name to another one..like DefaultConnectionString1 to DefaultConnectionString12 or 1000 ... and you can have multiple db context to work on multiple databases to...

in Code first from database is like that to ...so in each part of project you can make instanse of which dbcontext you like. and do CRUD operation on that..

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.