EF6在运行时使用无参数连接字符串设置连接字符串

时间:2016-10-01 12:52:41

标签: c# mysql .net entity-framework model-view-controller

我有一个自定义DbContext类,并在运行时从文本文件和DbContext连接字符串中读取连接字符串。例如;

[SuppressMessage("Microsoft.Design", "CA1063:ImplementIDisposableCorrectly")]
[DbConfigurationType(typeof(MySqlEFConfiguration))]
public partial class ASDbContext : IdentityDbContext<ASUser, ASRole, int, ASUserLogin, ASUserRole, ASUserClaim>, IDbContext
{
    private readonly IConnectionStringManager _connectionStringManager;

    public ASDbContext(IDatabaseInitializer<ASDbContext> dbInitializer,
        IDbCommandInterceptor dbCommandInterceptor,
        IConnectionStringManager connectionStringManager) : base() 
    {
        this._connectionStringManager = connectionStringManager;

        if (!_connectionStringManager.CheckIfConnectionStringExists())
        {
            return;
        }
        this.Database.Connection.ConnectionString = _connectionStringManager.ReadConnectionString().;
        System.Data.Entity.Database.SetInitializer(dbInitializer);
        this.Configuration.ProxyCreationEnabled = false;
        DbInterception.Add(dbCommandInterceptor);
    }
     //...                       
}

这适用于Sql Server。但是,它不适用于mysql。以下行抛出异常;

 this.Database.Connection.ConnectionString = _connectionStringManager.ReadConnectionString().ConnectionString;
  

[ArgumentException:初始化字符串的格式不符合从索引0开始的规范。]      System.Data.Common.DbConnectionOptions.GetKeyValuePair(String connectionString,Int32 currentPosition,StringBuilder buffer,Boolean useOdbcRules,String&amp; keyname,String&amp; keyvalue)+1739      System.Data.Common.DbConnectionOptions.ParseInternal(Hashtable parsetable,String connectionString,Boolean buildChain,Hashtable synonyms definitions,Boolean firstKey)+191      System.Data.Common.DbConnectionOptions..ctor(String connectionString,Hashtable synonyms definitions,Boolean useOdbcRules)+136      System.Data.Common.DbConnectionStringBuilder.set_ConnectionString(String value)+87      MySql.Data.MySqlClient.MySqlConnectionStringBuilder..ctor(String connStr)+81      MySql.Data.MySqlClient.MySqlConnection.set_ConnectionString(String value)+382      MySql.Data.Entity.MySqlConnectionFactory.CreateConnection(String connectionString)+38      System.Data.Entity.Internal.LazyInternalConnection.Initialize()+398      System.Data.Entity.Internal.LazyInternalConnection.get_Connection()+16

我提出的解决方法是将虚拟连接字符串传递给基本DbContext类的构造函数。如;

[SuppressMessage("Microsoft.Design", "CA1063:ImplementIDisposableCorrectly")]
[DbConfigurationType(typeof(MySqlEFConfiguration))]
public partial class ASDbContext : IdentityDbContext<ASUser, ASRole, int, ASUserLogin, ASUserRole, ASUserClaim>, IDbContext
{
    private readonly IConnectionStringManager _connectionStringManager;

    // Passing "S=;" as connection string is a dirty workaround
    public ASDbContext(IDatabaseInitializer<ASDbContext> dbInitializer,
        IDbCommandInterceptor dbCommandInterceptor,
        IConnectionStringManager connectionStringManager) : base("S=;") 
    {
        this._connectionStringManager = connectionStringManager;

        if (!_connectionStringManager.CheckIfConnectionStringExists())
        {
            return;
        }
        this.Database.Connection.ConnectionString = _connectionStringManager.ReadConnectionString().;
        System.Data.Entity.Database.SetInitializer(dbInitializer);
        this.Configuration.ProxyCreationEnabled = false;
        DbInterception.Add(dbCommandInterceptor);
    }
     //...                       
}

虽然,它的工作原理我不喜欢通过虚拟连接字符串来修复问题。 还有其他方法可以解决此错误吗?

0 个答案:

没有答案