IronAces
IronAces

Reputation: 1883

EF Code first Keyword not supported: 'provider'

I'm attempting to dynamically provide database credentials to my EF model. The below approach has worked in the past when using Database First. There are several similar SO questions however none seem to resolve this issue. What am I missing here?

private const string ProviderName = "System.Data.SqlClient";

var SqlConnectionStringBuilder = new SqlConnectionStringBuilder {
    DataSource = this.ServerName,
    InitialCatalog = this.DatabaseName,
    IntegratedSecurity = true
};
var EntityConnectionStringBuilder = new EntityConnectionStringBuilder {
    Provider = ProviderName,
    ProviderConnectionString = SqlConnectionStringBuilder.ToString()
};
using(var db = new AuditingContext(EntityConnectionStringBuilder.ToString())) 
{
    var session = new Session() {
    };
    db.Sessions.Add(session);
    //ArgumentException occurs here
    //Keyword not supported: 'provider'.
}

The DbContext

public class AuditingContext: DbContext {
    public DbSet <Session> Sessions { get; set; }
    public DbSet <Cause> Causes { get; set; }
    public AuditingContext(string connectionStringName): base(connectionStringName) {}
}

The connection string

provider=System.Data.SqlClient;provider connection string=\"Data Source=localhost;Initial Catalog=TEST_DATABASE;Integrated Security=True\"

Upvotes: 2

Views: 1962

Answers (3)

Farid Hatimi
Farid Hatimi

Reputation: 1

I was using a console application to test the Code First approach with Sql Server 2016. My initial connectionString was :

add name="MyCodeFirstDb" connectionString="
Data Source=MyServer;
Initial Catalog=MyCodeFirstDb;
Provider=SQLNCLI11.1;
Integrated Security=SSPI;
Auto Translate=False;"

I put my "connectionStrings" section after the "entityFramework" section.

The error I got when creating a linq query was :

"Keyword provider not recognized"

I removed "Provider=SQLNCLI11.1;"

I then got the error :

"Keyword auto-translate not recognized"

I removed "AutoTranslate=False;"

I was then able to run the query and my database was created.

Upvotes: 0

Dharmesh Kumar Laxkar
Dharmesh Kumar Laxkar

Reputation: 411

use Like that

<add name="TerminalA" connectionString="Data 
Source=192.168.1.104;Database=ABC;Integrated Security=false;User 
ID=sa;Password=pass@123;" providerName="System.Data.SqlClient" />

Upvotes: -1

IronAces
IronAces

Reputation: 1883

As pointed out by DevilSuichiro, the DbContext for EF5+ is a connection string and not EntityConnectionString. This resolves the issue.

var connString = "provider=System.Data.SqlClient;provider connection string=\"Data Source=localhost;Initial Catalog=TEST_DATABASE;Integrated Security=True\"";
using(var db = new AuditingContext(connString)) 
{
   //...
}

Upvotes: 3

Related Questions