2022年07月08日

EF使用PostgreSQL

#region PostgreSQL数据库连接
            
string connection = Configuration.GetConnectionString("PostgreSQL");
Console.WriteLine(connection);
services.AddDbContext<PostgreSQLDbContext>(option =>
                                           {
                                               option.UseNpgsql(connection);
                                           });

#endregion PostgreSQL数据库连接
/// <summary>
/// 上下文对象 EF 所需的Context
/// </summary>
public class PostgreSQLDbContext : DbContext
{
    public DbSet<Models.User> User { get; set; }

    /// <summary>
    /// 调用基类的构造法方法 public DbContext([NotNullAttribute] DbContextOptions options);
    /// </summary>
    /// <param name="options"></param>
    public PostgreSQLDbContext(DbContextOptions<PostgreSQLDbContext> options) : base(options)
    {

    }
}
/// <summary>
/// 用户实体类
/// </summary>
[Table("user")]
public class User
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
}
 <li class="nav-item">
     <a class="nav-link text-dark" asp-area="" asp-controller="Users" asp-action="Index">用户</a>
</li>
{
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "AllowedHosts": "*",
  "ConnectionStrings": {
    "PostgreSQL": "Host=127.0.0.1;Username=postgres;Password=123456;Database=hjy"
  }
}

未成功