c# - How to change the database name in Entity Framework -
i'm trying find way change database name in web.config
, context. no other info in connection string changes database name.
public apicontext(string dbname = "myfirstdb") : base("originalcontext") { this.database.connection.connectionstring = this.database.connection.connectionstring.replace("myfirstdb", dbname); }
the way can find achieve replace name, can see few problems in future, example if need go or need point database. using mysql.
any 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 work if call "setdatabasename(string name)" method before call context class?
i'm confused on should replacing database name , how it.
have tried this?
public partial class mymodel : dbcontext { public mymodel() : base("name=mymodeldatacontext") // <-- connstring name { } }
and connstring looks this
<add name="mymodeldatacontext" connectionstring="data source=... initial catalog=yourdb
this way can change db name time.
this how code first database wizard generates it
Comments
Post a Comment