要建立在Entity Framework 6 和Framework 6 和連接,請依照以下步驟操作:
<add name="mysqlCon" connectionString="Server=localhost;Database=dbName;Uid=username;Pwd=password" providerName="MySql.Data.MySqlClient" />
<defaultConnectionFactory type="MySql.Data.Entity.MySqlConnectionFactory, MySql.Data.Entity.EF6" /> <providers> <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6" /> </providers>
<add name="mysqlCon" connectionString="Server=localhost;Database={0};Uid=username;Pwd=password" providerName="MySql.Data.MySqlClient" />
public class ApplicationDbContext : DbContext { public ApplicationDbContext(string dbName) : base(GetConnectionString(dbName)) { } public static string GetConnectionString(string dbName) { var connString = ConfigurationManager.ConnectionStrings["mysqlCon"].ConnectionString.ToString(); return String.Format(connString, dbName); } }
public class MigrationsContextFactory : IDbContextFactory<ApplicationDbContext> { public ApplicationDbContext Create() { return new ApplicationDbContext("developmentdb"); } }
這允許您將動態連接字串傳遞到實體框架上下文,使您能夠根據帳戶輕鬆地在多個相同的資料庫模式之間切換。
以上是如何使用 MySQL 連線到 Entity Framework 6 中的動態資料庫名稱?的詳細內容。更多資訊請關注PHP中文網其他相關文章!