Npgsql System.TypeInitializationException

Viewed 11

I'm trying to use Npgsql with entity framework 6 and I get the following error

System.TypeInitializationException
  HResult=0x80131534
  Message=The type initializer for 'System.Data.Entity.Internal.AppConfig' threw an exception.
  Source=EntityFramework
  StackTrace:
   at System.Data.Entity.Internal.AppConfig.get_DefaultInstance()
   at System.Data.Entity.Internal.LazyInternalConnection..ctor(DbContext context, String nameOrConnectionString)
   at System.Data.Entity.DbContext..ctor()
   at EF_ModelFirst.BloggingContext..ctor()
   at Program.Main(String[] args) in C:\Users\brandp3.DTINET\source\repos\EF-ModelFirst\EF-ModelFirst\Program.cs:line 7

  This exception was originally thrown at this call stack:
    [External Code]

Inner Exception 1:
ConfigurationErrorsException: Configuration system failed to initialize

Inner Exception 2:
ConfigurationErrorsException: Unrecognized configuration section entityFramework. (C:\Users\brandp3.DTINET\source\repos\EF-ModelFirst\EF-ModelFirst\bin\Debug\net6.0\EF-ModelFirst.dll.config line 6)

Here's my app.config. This is copied from the npgsql guide for EF6. The only difference is that the connection string has been modified to point to a local postgres container

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <connectionStrings>
        <add name="BlogDbContext" connectionString="Server=localhost;port=5432;Database=Blog;User Id=postgres;Password=mysecretpassword;" providerName="Npgsql" />
    </connectionStrings>
    <entityFramework>
        <providers>
            <provider invariantName="Npgsql" type="Npgsql.NpgsqlServices, EntityFramework6.Npgsql" />
        </providers>
        <!-- setting the default connection factory is optional -->
        <defaultConnectionFactory type="Npgsql.NpgsqlConnectionFactory, EntityFramework6.Npgsql" />
    </entityFramework>
    <system.data>
        <DbProviderFactories>
            <add name="Npgsql Provider" invariant="Npgsql" description=".NET Framework Data Provider for PostgreSQL" type="Npgsql.NpgsqlFactory, Npgsql, Version=4.1.3.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7" />
        </DbProviderFactories>
    </system.data>
</configuration>

The error is thrown on this line

using (var db = new BloggingContext())
0 Answers
Related