title: Entity Framework 5.0 PostgreSQL (Npgsql) default connection factory
body: <p>I'm trying to get EF 5.0 code first working with PostgreSQL (Npgsql provider). I have Npgsql 2.0.12.1 installed via NuGet (referenced assembly is 2.0.12.0 though).
  I have Npgsql declared in app.config (both default connection factory and provider factory):  </p>
  
  <pre><code>&lt;entityFramework&gt;   
      &lt;defaultConnectionFactory type="Npgsql.NpgsqlFactory, Npgsql, Version=2.0.12.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7" /&gt;
   &lt;/entityFramework&gt;
   &lt;system.data&gt;
          &lt;DbProviderFactories&gt;
            &lt;add name="Npgsql Data Provider"
                  invariant="Npgsql"
                  description="Data Provider for PostgreSQL"
                  support="FF"
                  type="Npgsql.NpgsqlFactory, Npgsql, Version=2.0.12.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7"/&gt;
          &lt;/DbProviderFactories&gt;
   &lt;/system.data&gt;
  </code></pre>
  
  <p>I have following test running successfully :</p>
  
  <pre><code>[Test]
  public void DatabaseConnection_DatabaseFactoryTest()
  {
      var factory = DbProviderFactories.GetFactory("Npgsql");
      var conn = factory.CreateConnection();
      conn.ConnectionString = _connectionString;
      var npg = (NpgsqlConnection)conn;
      var result = TestConnectionHelper(npg); // scalar select version(), nothing particular
      Assert.AreEqual(result, "PostgreSQL 9.2.2, compiled by Visual C++ build 1600, 64-bit");            
  }
  </code></pre>
  
  <p>That means at least database instance is running and provider is configured successfully.
  Now what i want is to use custom database context inherited from DbContext which will be tied to same provider and initialized via connection string :</p>
  
  <pre><code>public class InventoryContext : DbContext
  {
      public InventoryContext(string nameOrConnectionString) : base(nameOrConnectionString)
      {
      }
      // mappings and properties, cut for conciseness
  }
  </code></pre>
  
  <p>Following test fails :</p>
  
  <pre><code>[Test]
  public void DatabaseConnection_DatabaseContextTest()
  {
      using (var ctx = new InventoryContext(_connectionString))
      {
          //var db = ctx.Database;
          ctx.InventoryObjects.Add(_inventoryObject); // exception here
          ctx.SaveChanges();
      }
  }   
  </code></pre>
  
  <p>It says</p>
  
  <pre><code>Failed to set Database.DefaultConnectionFactory to an instance of the 'Npgsql.NpgsqlFactory, Npgsql, Version=2.0.12.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7' type as specified in the application configuration. See inner exception for details.
  </code></pre>
  
  <p>Inner exception is InvalidOperationException :</p>
  
  <pre><code>{"Constructor for type \"Npgsql.NpgsqlFactory\" is not found."}
  </code></pre>
  
  <p>I guess there is a problem with connection string (it does not contain Npgsql provider) : </p>
  
  <pre><code>"Server=127.0.0.1;Port=5432;User Id=postgres;Password=p4ssw0rd;Database=InventoryDatabase;";
  </code></pre>
  
  <p>What is the most elegant way to solve this problem programmatically? Just tried passing connectionString from app.config to context's constructor, it works.<br/>
  <strong>edit</strong><br/>
  Uploaded test project to Dropbox - <a href="https://dl.dropbox.com/u/14800693/Median.Inventory.zip">VS2012 solution, 10 mb</a></p>