Home > Database > Mysql Tutorial > body text

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

Barbara Streisand
Release: 2024-11-11 04:14:03
Original
222 people have browsed it

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>
Copy after login

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);
    }
}
Copy after login

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");
    }
}
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template