【发布时间】:2019-03-21 17:28:26
【问题描述】:
刚刚为测试数据库制作了新的控制台应用程序。过去在我的项目中每次都运行良好,但现在我遇到了无法解决的问题。我手动创建了 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.'
【问题讨论】:
标签: c# postgresql visual-studio entity-framework