Home >Database >Mysql Tutorial >How to Achieve Dynamic Database Connection with Entity Framework 6 for MySQL?

How to Achieve Dynamic Database Connection with Entity Framework 6 for MySQL?

Barbara Streisand
Barbara StreisandOriginal
2024-11-11 04:14:03301browse

How to Achieve Dynamic Database Connection with Entity Framework 6 for MySQL?

Dynamic Database Connection with Entity Framework 6 for MySQL

Connecting multiple schemas dynamically with Entity Framework 6 (EF6) can be challenging. This article provides a comprehensive solution to establish a dynamic connection to MySQL databases and pass the connection string based on the selected database name.

Getting MySQL Ready for EF6

Begin by installing the MySQL .Net Connector 6.8.1 (Beta) and reference the libraries in your Visual Studio solution. Add a connection string and provider information to the Web.config file:

<connectionStrings>
    <add name="mysqlCon"
         connectionString="Server=localhost;Database={0};Uid=username;Pwd=password" 
         providerName="MySql.Data.MySqlClient" />
</connectionStrings>

<entityFramework>
    <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>
</entityFramework>

Creating a Dynamic Database Connection

Next, modify the ApplicationDbContext class:

public class ApplicationDbContext: DbContext
{
    public ApplicationDbContext(string dbName) : base(GetConnectionString(dbName))
    {
    }

    public static string GetConnectionString(string dbName)
    {
        // Server=localhost;Database={0};Uid=username;Pwd=password
        var connString = 
            ConfigurationManager.ConnectionStrings["mysqlCon"].ConnectionString.ToString();

        return String.Format(connString, dbName);
    }
}

This allows passing the database name dynamically as a parameter when creating a new ApplicationDbContext instance.

Handling Database Migrations

For migrations, create a MigrationsContextFactory class:

public class MigrationsContextFactory : IDbContextFactory<ApplicationDbContext>
{
    public ApplicationDbContext Create()
    {
        return new ApplicationDbContext("developmentdb");
    }
}

This factory specifies a default database for migrations, ensuring the migrations target the correct schema.

Conclusion

Utilizing this approach enables the dynamic selection of database schemas in Entity Framework 6. By modifying the default connection factory and creating a helper method to dynamically generate the connection string, it is possible to connect to multiple schemas efficiently.

The above is the detailed content of How to Achieve Dynamic Database Connection with Entity Framework 6 for MySQL?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn