首页 > 解决方案 > 如何创建指定数据库类型的 DbContext 实例

问题描述

我知道这里默认返回的是SQL Server数据库类型,但是我需要MySQL数据库类型,问题是创建DbContext指定数据库类型的实例?

当然我知道也可以通过指定 来创建connectionName,但是数据库连接字符串是加密的,字符串需要解密

伪代码如下:

public partial class MyDbContext : DbContext
{
    static string mysqlConn = ReadConnectionString(); //Get the encrypted MySQL connection string

    public MyDbContext() : base(mysqlConn)
    {
        // Even if I want to modify the connection string in this way, nor will it throw a connection string malformed exception
        Database.Connection.ConnectionString = DecryptConnectionString(mysqlConn);
    }
}

标签: c#entity-framework

解决方案


Ok, it's about what I expected, few of my questions can be answered.

Although I am not too familiar with EntityFramework, by looking at the source code of DbContext, I can almost certainly determine that this is a design flaw, it lacks a constructor like this:


Public DbContext(string connectionString, string providerName)


推荐阅读