2

I'm trying to find a way to change the database name in my web.config and my context. No other info in my connection string changes but the database name.

 public APIContext(string dbname = "MyFirstDB") : base("OriginalContext")
 {
     this.Database.Connection.ConnectionString = this.Database.Connection.ConnectionString.Replace("MyFirstDB", dbname);
 }

The only way I can find to achieve this is to replace the name, but I can see few problems in the future, for example if I need to go back or need to point to another database. Using mysql.

Any help will be appreciated.

** EDIT **

[DbConfigurationType(typeof(MySql.Data.Entity.MySqlEFConfiguration))]
public class APIContext : DbContext
{
    public APIContext() : base("MyContext")
    {
    }

    public void setDatabaseName(string name)
    {
        var currentdatabase = this.Database.Connection.Database;
        this.Database.Connection.ConnectionString = this.Database.Connection.ConnectionString.Replace(currentdatabase, name);
    }

Would this work if I were to call the "setDatabaseName(string name)" method before I call my context class?

I'm just confused on where I should be replacing the database name and how to do it.

brachen33
  • 117
  • 2
  • 10
  • *would this work* why dont you just try and see if it works? I mean it is faster to try it then to ask here and wait for an answer. Why cant you just change the name of the db in connection string in config file: `Database=whateverDb`? – CodingYoshi Sep 12 '17 at 16:42
  • I've tried this Yoshi. Where I'm confused is how to set it up and where I need to change the database name in the connection string. After I hit my initial database, I get a database name, and would like to switch to that database. I don't know how to do it. – brachen33 Sep 12 '17 at 17:12
  • 1
    See [this answer](https://stackoverflow.com/a/20294903/4228458) – CodingYoshi Sep 12 '17 at 17:19
  • Just what I was looking for. Thank you Yoshi. – brachen33 Sep 12 '17 at 17:53

2 Answers2

4

Have you tried this?

public partial class MyModel : DbContext
    {
    public MyModel()
                : base("name=MyModelDataContext") // <-- ConnString Name
            {
            }
}

And your connstring looks like this

<add name="MyModelDataContext" connectionString="data source=...  initial catalog=YOURDB

This way you can change your db name any time.

this is actually how the code first from database wizard generates it

Victor Hugo Terceros
  • 2,969
  • 3
  • 18
  • 31
  • Thank you Victor, but this is where I'm confused. How do I just change the database name with this? – brachen33 Sep 12 '17 at 02:08
  • ok, whats is hardcoded here is the connstring name, the **database name** is in the web.config, in my sample code it is Initial Catalog = **YOURDBNAME** since the web.config is editable any time, that means that your db name can be changed at any time. – Victor Hugo Terceros Sep 12 '17 at 02:32
  • Thanks again Victor. But what is my best bet on changing the dbname? I switch my web.config over to using the 'Initial Catalog=""' instead of 'Database=""'. How might I change that database name without using replace? – brachen33 Sep 12 '17 at 15:32
  • Ok, yes in your case since you use MySql you have to use this kind of connstring _Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;_ and just replace **myDataBase**, just that, that is the only place the dbname should be. – Victor Hugo Terceros Sep 12 '17 at 15:41
  • I've edited my Context class above, but I still feel like I'm on the wrong path. – brachen33 Sep 12 '17 at 16:04
1

Ok, if you want to use more than one connstring you can do this

public class MyModel : DbContext
{
    public MyModel () 
      : base(ApplicationParameters.ConnectionStringName)
    {
    }

    public MyModel (string connectionStringName)
      : base(connectionStringName)
    {
    }

}

Then in your web.config you can set a list of connstrings and in your code you can call any of them with the second constructor.

Victor Hugo Terceros
  • 2,969
  • 3
  • 18
  • 31