1

I'm trying to pass a connection string to DbContext class through it's constructor but I keep getting this exception. System.ArgumentException: 'Keyword not supported: 'provider'.'

Based on other stackoverflow posts I'm getting the connection string this way.

public string GetConnection()
{
    SqlConnectionStringBuilder sqlBuilder = new SqlConnectionStringBuilder();
    sqlBuilder.DataSource = "DataSource";
    sqlBuilder.InitialCatalog = "InitialCatalog";
    sqlBuilder.UserID = "UserID";
    sqlBuilder.Password = "Password";

    EntityConnectionStringBuilder entityString = new EntityConnectionStringBuilder()
    {
        Provider = "System.Data.SqlClient",
        ProviderConnectionString = sqlBuilder.ToString()
    };

    return entityString.ConnectionString;
}

This is what is generated (actual credentials replaced with dummy data)

provider=System.Data.SqlClient;provider connection string="Data Source=DataSource;Initial Catalog=InitialCatalog;User ID=UserID;Password=Password"

This is the DBContext class constructor

    public CustomerContext(string connectionString) : base(connectionString)
    {

    }
dom-dev
  • 91
  • 6
  • Just tried it, unfortunately getting the same error. I've also tried sending just the connection string but EF6 throws an exception stating No provider was included. – dom-dev May 05 '20 at 21:18

1 Answers1

1

For anyone who that might be running into this error. Turns out I had to install the EF6 packages on all projects that were going to reference my repository dll.

Once the packages are installed on all projects that need it, I passed in the connection string only. Without any special formatting or providing the Provider info.

Data Source=DataSource;Initial Catalog=InitialCatalog;User ID=UserID;Password=Password
dom-dev
  • 91
  • 6