title: Entity Framework 6 - Npgsql - connection string error
body: <p>A tricky (and frustrating problem) - perhaps you folks may be clever enough to solve it:</p>
  
  <p><strong>Problem</strong></p>
  
  <p>I want to be able to read/write to my database using Entity Frameworks. I have got a simple app rails running on Heroku (straightforward scaffold). I want to connect to this database and manipulate records. The good news is that I can successfully connect to that database using npgsql. The bad news is that I cannot do it with Entity Frameworks. This is the error I’m receiving:</p>
  
  <blockquote>
    <p>System.Data.Entity.Core.ProviderIncompatibleException: An error
    occurred while getting provider information from the database. This
    can be caused by Entity Framework using an incorrect connection
    string. Check the inner exceptions for details and ensure that the
    connection string is correct. --->
    System.Data.Entity.Core.ProviderIncompatibleException: The provider
    did not return a ProviderManifestToken string. --->
    System.IO.FileLoadException: Could not load file or assembly 'Npgsql,
    Version=3.1.2.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7' or
    one of its dependencies. The located assembly's manifest definition
    does not match the assembly reference. (Exception from HRESULT:
    0x80131040)</p>
  </blockquote>
  
  <p>Here is the stack trace:</p>
  
  <pre><code>   at Npgsql.NpgsqlServices.GetDbProviderManifestToken(DbConnection connection) 
     at System.Data.Entity.Core.Common.DbProviderServices.GetProviderManifestToken(DbConnection connection) 
     --- End of inner exception stack trace --- 
     at System.Data.Entity.Core.Common.DbProviderServices.GetProviderManifestToken(DbConnection connection) 
     at System.Data.Entity.Utilities.DbProviderServicesExtensions.GetProviderManifestTokenChecked(DbProviderServices providerServices, DbConnection connection) 
     --- End of inner exception stack trace --- 
     at System.Data.Entity.Utilities.DbProviderServicesExtensions.GetProviderManifestTokenChecked(DbProviderServices providerServices, DbConnection connection) 
     at System.Data.Entity.Infrastructure.DefaultManifestTokenResolver.&lt;&gt;c__DisplayClass1.&lt;ResolveManifestToken&gt;b__0(Tuple`3 k) 
     at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory) 
     at System.Data.Entity.Infrastructure.DefaultManifestTokenResolver.ResolveManifestToken(DbConnection connection) 
     at System.Data.Entity.Utilities.DbConnectionExtensions.GetProviderInfo(DbConnection connection, DbProviderManifest&amp; providerManifest) 
     at System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection) 
     at System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext) 
     at System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input) 
     at System.Data.Entity.Internal.LazyInternalContext.InitializeContext() 
     at System.Data.Entity.Internal.InternalContext.Initialize() 
     at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) 
     at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize() 
     at System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext() 
     at System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider() 
     at System.Linq.Queryable.Select[TSource,TResult](IQueryable`1 source, Expression`1 selector) 
     at ge_EntityFrameworkTest.Program.&lt;Test&gt;d__4.MoveNext() in c:\Users\Koshy\Documents\Visual Studio 2013\Projects\Practice\ge-EntityFrameworkTest\ge-EntityFrameworkTest\Program.cs:line 118
  </code></pre>
  
  <p>Here is my connection string:</p>
  
  <pre><code>NpgsqlConnectionStringBuilder sqlBuilder = new NpgsqlConnectionStringBuilder();
                  sqlBuilder.Username = user;
                  sqlBuilder.Password = password;
                  sqlBuilder.Host = host;
                  sqlBuilder.Port = Int32.Parse(port);
                  sqlBuilder.Database = database;
                  sqlBuilder.Pooling = true;
                  sqlBuilder.UseSslStream = true;     
                  sqlBuilder.SslMode = Npgsql.SslMode.Require;
                  sqlBuilder.TrustServerCertificate = true;
  </code></pre>
  
  <p>Here is my “Hello world” that I am using to connect and read from my database (from the players table). It is successfully printing: “Lionel Messi” on to the console. Great!</p>
  
  <pre><code>            #region connectingAndReadingDatabase
              using (var conn = new NpgsqlConnection(sqlBuilder.ToString()))
              {
                  conn.Open();
                  using (var cmd = new NpgsqlCommand())
                  {
                      cmd.Connection = conn;
  
                      // Retrieve all rows
                      cmd.CommandText = "SELECT * FROM players";
                      using (var reader = cmd.ExecuteReader())
                      {
                          while (reader.Read())
                          {
                              Console.WriteLine(reader.GetString(1));
                          }
                      }
                  }
              }
              #endregion
  </code></pre>
  
  <p>The problem is when I try to use Entity Frameworks. It fails massively with a painful error. I am using exactly the same connection string, and cannot for the life of me work out where I’m going wrong. Perhaps you may be able to easily spot the problem?</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Text.RegularExpressions;
  using System.Threading.Tasks;
  using System.Net.Http;
  using Newtonsoft.Json;
  using Npgsql;
  using System.Data.Entity;
  using System.Data.Common;
  using System.ComponentModel.DataAnnotations.Schema;
  using System.ComponentModel.DataAnnotations;
  using System.Configuration;
  using System.Data.Entity.ModelConfiguration.Conventions;
  
  
  // Here is the code pertaining to my hello world entity framework example:
  
          [Table("players", Schema = "public")]
          public  class Player
          {
              [Key]
              [Column("id")]
              public int id { get; set; }
               [Column("name")]
              public string Name { get; set; }
               [Column("team")]
              public string Team { get; set; }
              public Player() { }
          }
  
          class NpgsqlConfiguration : System.Data.Entity.DbConfiguration
          {
              public NpgsqlConfiguration()
              {
                  SetProviderServices ("Npgsql", Npgsql.NpgsqlServices.Instance);
                  SetProviderFactory ("Npgsql", Npgsql.NpgsqlFactory.Instance);
                  SetDefaultConnectionFactory (new Npgsql.NpgsqlConnectionFactory ());
              }
          }
  
          [DbConfigurationType(typeof(NpgsqlConfiguration))]
          public class PlayerContext : DbContext
          {     
              public PlayerContext(DbConnection connection): base(connection, true)
              {                
              }
  
              public DbSet&lt;Player&gt; Players { get; set; }
              protected override void OnModelCreating(DbModelBuilder modelBuilder)
              {
  
                  modelBuilder.Conventions.Remove&lt;OneToManyCascadeDeleteConvention&gt;();
                  //modelBuilder.Conventions.Add&lt;CascadeDeleteAttributeConvention&gt;();
                  modelBuilder.Conventions.Remove&lt;PluralizingTableNameConvention&gt;();
                  modelBuilder.HasDefaultSchema("public");
                  base.OnModelCreating(modelBuilder);
              }           
          }
  </code></pre>
  
  <p>And here is my app.config file</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;configuration&gt;
    &lt;configSections&gt;
      &lt;!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --&gt;
      &lt;section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /&gt;
    &lt;/configSections&gt;
    &lt;startup&gt;
      &lt;supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /&gt;
    &lt;/startup&gt;  
    &lt;entityFramework&gt;
      &lt;defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"&gt;
        &lt;parameters&gt;
          &lt;parameter value="v12.0" /&gt;
        &lt;/parameters&gt;
      &lt;/defaultConnectionFactory&gt;
      &lt;providers&gt;
        &lt;provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /&gt;
        &lt;provider invariantName="Npgsql" type="Npgsql.NpgsqlServices, EntityFramework6.Npgsql" /&gt;
      &lt;/providers&gt;    
    &lt;/entityFramework&gt;
    &lt;runtime&gt;
      &lt;assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"&gt;
        &lt;dependentAssembly&gt;
          &lt;assemblyIdentity name="Npgsql" publicKeyToken="5d8b90d52f46fda7" culture="neutral" /&gt;
          &lt;bindingRedirect oldVersion="0.0.0.0-3.1.0.0" newVersion="3.1.0.0" /&gt;
        &lt;/dependentAssembly&gt;
      &lt;/assemblyBinding&gt;
    &lt;/runtime&gt;
    &lt;system.data&gt;
      &lt;DbProviderFactories&gt;
        &lt;add name="Npgsql Data Provider"
              invariant="Npgsql"
              description="Data Provider for PostgreSQL"
              type="Npgsql.NpgsqlFactory, Npgsql" /&gt;
      &lt;/DbProviderFactories&gt;
    &lt;/system.data&gt;
    &lt;connectionStrings&gt;
      &lt;add name="PlayerContext" connectionString="Username=hjanadgkizjmgf;Password=password;Host=ec2-54-235-250-156.compute-1.amazonaws.com;Port=5432;Database=deek4ap6cf2a1;Pooling=true;Use SSL Stream=True;SSL Mode=Require;TrustServerCertificate=True;" providerName="Npgsql" /&gt;
    &lt;/connectionStrings&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p>When I pass in a connection string directly -  the same one which worked so well to retrieve records earlier, I get this curious exception: </p>
  
  <blockquote>
    <p>“Keyword not supported ‘username’” – obviously referring to the
    connection string passed in.</p>
  </blockquote>
  
  <pre><code>using (var db = new PlayerContext(sqlBuilder.ToString()))
                  { // etc }
  </code></pre>
  
  <p>Also curiously, I receive a warning before compiling:</p>
  
  <blockquote>
    <p>“Warning 1   Found conflicts between different versions of the same
    dependent assembly that could not be resolved.  These reference
    conflicts are listed in the build log when log verbosity is set to
    detailed. pg-EF-test2” perhaps it’s something to do with Npgsql?</p>
  </blockquote>
  
  <p>Any assistance would be much appreciated.</p>