npgsql and Entity Framework code first setup problems

c# ef-code-first entity-framework npgsql

Question

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.

1
4
4/7/2013 4:39:30 AM

Accepted 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.

13
4/30/2013 1:15:55 PM

Popular Answer

I agree with the previous answer by iwanek. To be a bit more specific, I like to place the statement in the OnModelCreating override method so that it is always called when the context is created.

    public class MyDbContext : DbContext
    {
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            Database.SetInitializer<MyDbContext>(null);
        }
    }


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow