简体   繁体   中英

Npgsql C# problem with App.Config PostgreSQL Entity Framework 6

Just made new Console App for test database. It was working fine every time in my projects in past, but now i have problem which i can't solve. I created App.config manualy (VStudio didn't generate it). I used settings from This link . In past i was working with MSSql, and SQLite and it works fine. But my try with Postgresql not working. Can you tell me what should i do?

My App.config file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <!-- ... -->

  <entityFramework>
    <providers>
      <provider invariantName="Npgsql"
         type="Npgsql.NpgsqlServices, EntityFramework6.Npgsql" />
    </providers>
  </entityFramework>

  <system.data>
    <DbProviderFactories>
      <add name="Npgsql Data Provider"
           invariant="Npgsql"
           description="Data Provider for PostgreSQL"
           type="Npgsql.NpgsqlFactory, Npgsql"
           support="FF" />
    </DbProviderFactories>
  </system.data>

  <connectionStrings>
    <add name="AppDatabaseConnectionString"
         connectionString="Server=localhost;Database=photo_service;Port=5432;Password=postgres;User Id=postgres"
         providerName="Npgsql" />
  </connectionStrings>

</configuration>

My AppDatabase:Context and class User

class AppDatabase : DbContext
{
    private readonly string schema;
    public AppDatabase(string schema) : base("AppDatabaseConnectionString")
    {
        this.schema = schema;
    }

    public DbSet<User> Users { get; set; }

    protected override void OnModelCreating(DbModelBuilder builder)
    {
         builder.HasDefaultSchema(this.schema);
         base.OnModelCreating(builder);
    }


    }

class User
 {
    public int id { get; set; }
    public string name { get; set; }
    public string number { get; set; }
 }

And finally my initialization of postgres query:

var db = new AppDatabase("Users");
using (var database = new AppDatabase("User"))
    {
        var users = database.Users.First(user => user.name == "Blah blah blah");
    }

it all gives me error:

Unable to determine the provider name for provider factory of type 
'System.Data.SqlClient.SqlClientFactory'. Make sure that the ADO.NET provider is 
installed or registered in the application config.'

Okay, solved. I created ConsoleApp .NET Framework instead ConsoleApp .NET Core as github says

EF6 does not (yet) run on .NET Core--see

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM