Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

npgsql and Entity Framework code first setup problems

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.

like image 867
Ankou Avatar asked Sep 22 '12 15:09

Ankou


People also ask

How do I use code first in Entity Framework?

Step 1 − First, create the console application from File → New → Project… Step 2 − Select Windows from the left pane and Console Application from the template pane. Step 3 − Enter EFCodeFirstDemo as the name and select OK. Step 4 − Right-click on your project in the solution explorer and select Manage NuGet Packages…

Does PostgreSQL work with Entity Framework?

Using the Entity Data ProviderdotConnect for PostgreSQL allows using it in Entity Framework models in various ways. You can use our provider with standard Visual Studio Entity Framework tools, in the same way as SqlClient.


2 Answers

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.

like image 196
iwanek Avatar answered Nov 15 '22 21:11

iwanek


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);
        }
    }
like image 43
JK Dennis Avatar answered Nov 15 '22 22:11

JK Dennis