Npgsql and Entity Framework first setup problems

The most recent error I get is

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

but I am convinced that this happens only because something has not been set up correctly before.

I am currently trying to configure the code for entity framework 4, first use Npgsql 2.0.12, I did the following and it seems to at least now connect to the database, but giving me the above error when I do context.saveChanges ();

  • Updated machine.config file 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 DLL to the project

  • Changed app.config 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 transfer is as follows

    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 common with the context settings shelf

Any help on what I am doing wrong, tips or tutorials, everything will be helpful. This was just a little proof of concept, but I would not want it to work.

Ps sorry for the poor use of code formatting, stack sharing will not allow me to use it properly for some reason, even if it is formatted correctly.

+4
source share
2 answers

Npgsql does not support schema creation, so you need to create db manually. Then, to avoid this error, add this operator somewhere in your code (in your case, this may be at the beginning of the Main () function):

 Database.SetInitializer<DataContext>(null); 

Use a DbContext implementation instead of a DataContext.

+10
source

I agree with iwanek's previous answer. To be more specific, I like to put 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); } } 
+5
source

Source: https://habr.com/ru/post/1435670/


All Articles