C# – npgsql and Entity Framework code first setup problems

cef-code-firstentity-frameworknpgsql

The most recent error im getting is

ERROR: 42P01: relation "dbo.__MigrationHistory" does not exist

but im convinced that this is just because something earlier hasnt been set up properly.

Im currently trying to set up entity framework 4.4 code first to use Npgsql 2.0.12, I have done the following and it seems to atleast be connecting to the database now but giving me the above error when I do context.saveChanges();

  • Updated the machine.config for .net 2.0.50727 with;

    < add name="Npgsql Data Provider" invariant="Npgsql" support="FF"
    description=".Net Framework Data Provider for Postgresql Server"
    type="Npgsql.NpgsqlFactory, Npgsql, Version=2.0.12.0, Culture=neutral,
    PublicKeyToken=5d8b90d52f46fda7" />

  • Added the dlls to the project

  • Changed the app.config to look like this;

    <configuration>
      <configSections>
        <section name="entityFramework"
          type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection,
          EntityFramework, 
          Version=4.4.0.0, Culture=neutral, 
          PublicKeyToken=b77a5c561934e089" 
          requirePermission="false" />
      </configSections>
      <system.data>
        <DbProviderFactories>
          <remove invariant="Npgsql"></remove>
          <add name="Npgsql Data Provider" 
               invariant="Npgsql" 
               description=".Net Framework Data Provider for Postgresql Server" 
               type="Npgsql.NpgsqlFactory, Npgsql, 
                     Version=2.0.12.0, Culture=neutral, 
                     PublicKeyToken=5d8b90d52f46fda7" />
        </DbProviderFactories>
      </system.data>
      <connectionStrings>
        <add name="DataContext" 
             connectionString="Server=127.0.0.1;Port=5432;Database=postgres;User Id=postgres;Password=*******;CommandTimeout=20;" 
             providerName="Npgsql" />
      </connectionStrings>
    </configuration>
    
  • Data passing in looks like the following

    public class Animal
    {
    [Key]
    public int Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public int Age { get; set; }
    public int NoOfLegs { get; set; }
    }

  • Everything else is generic off the shelf set up of contexts

Any help on what I'm doing wrong or tip or tutorials, anything would be helpful. This was just a little proof of concept but I wouldnt mind getting it working.

P.s. sorry for the bad use of code formatting, stack exchange will not let me use it properly for some reason even though its formatted correctly.

Best Answer

Npgsql doesn't support schema creation, so you have to create db manually. Then to avoid this error add this statement somewhere in your code (in your case it might be on the beginning of Main() function):

Database.SetInitializer<DataContext>(null);

Instead of DataContext use your DbContext implementation.

Related Topic