App.Config PostgreSQL实体框架6的Npgsql C#问题

时间:2019-03-21 17:28:27

标签: c# postgresql visual-studio entity-framework

刚刚制作了用于测试数据库的新控制台应用程序。过去我的项目每次都运行良好,但是现在我遇到了无法解决的问题。我手动创建了App.config(VStudio不会生成它)。我使用了This link中的设置。过去我使用过MSSql和SQLite,并且工作正常。但是我对Postgresql的尝试不起作用。你能告诉我怎么办吗?

我的 App.config 文件:

<?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>

我的 AppDatabase:Context 类用户

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; }
 }

最后是我对postgres查询的初始化:

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

这一切都给我错误:

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

1 个答案:

答案 0 :(得分:0)

好的,解决了。我创建 ConsoleApp .NET Framework 代替 ConsoleApp .NET Core 如github所说

  

EF6尚未(尚未)在.NET Core上运行-请参见