刚刚制作了用于测试数据库的新控制台应用程序。过去我的项目每次都运行良好,但是现在我遇到了无法解决的问题。我手动创建了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.'
答案 0 :(得分:0)
好的,解决了。我创建
ConsoleApp .NET Framework
代替
ConsoleApp .NET Core
如github所说
EF6尚未(尚未)在.NET Core上运行-请参见