Reputation: 384
When I try to connect with the constructor that takes a ConStr parameter, the application throws exception below. But if I use the first constructor that takes no constr, connects successfully.
Thrown Exception Message:
Failed to set Database.DefaultConnectionFactory to an instance of the 'Npgsql.NpgsqlFactory, Npgsql' type as specified in the application configuration. See inner exception for details.
Inner Exception Message
Constructor on type 'Npgsql.NpgsqlFactory' not found.
Entity Framework - Version 6.1.3,
EntityFramework6.Npgsql - Version 3.1.1,
Npgsql - Version 3.1.7
Upvotes: 2
Views: 1213
Reputation: 1515
Include the following within your DbContext implementation namespace:
using Npgsql;
using System.Data.Entity;
class NpgSqlConfiguration : DbConfiguration
{
public NpgSqlConfiguration()
{
var name = "Npgsql";
SetProviderFactory(providerInvariantName: name,
providerFactory: NpgsqlFactory.Instance);
SetProviderServices(providerInvariantName: name,
provider: NpgsqlServices.Instance);
SetDefaultConnectionFactory(connectionFactory: new NpgsqlConnectionFactory());
}
}
Instantiate your context this way within your dependency injection or wherever you decide to create the context:
MyDbContext _dbContext;
var connectionString = Config.GetConnectionString("MyConnectionStringName");
NpgsqlConnection connection = new Npgsql.NpgsqlConnection(connectionString);
_dbContext = new MyDbContext(connection);
Upvotes: 1