无法使用 Npgsql.EntityFrameworkCore.PostgreSQL 查询超过几行 (3.1.4)

Unable to query beyond a few rows with Npgsql.EntityFrameworkCore.PostgreSQL (3.1.4)

我在 .NET Core MVC 3.1 项目中,使用 Npgsql.EntityFrameWorkCore.PostgreSQL (3.1.4) 并将连接字符串配置为远程 postgreSQL 数据库服务器。我没有使用 DBContext,因为我需要编写 SQL 查询。我在 HomeConroller 中有一个常规操作方法:

public IActionResult Countries()
        {
            _logger.LogInformation("About to return countries...");
            return View(DataAccess.GetCountries);
        }

我让国家使用

class DataAccess
    {
        public static List<Country> GetCountries
        {
            get {
                var connString = "connection string obscured";
                using var conn = new NpgsqlConnection(connString);
                conn.Open();
                var cmd = new NpgsqlCommand("SELECT name, capital, population FROM country", conn);
                List<Country> temp = new List<Country>();
                using (var reader = cmd.ExecuteReader())
                    while (reader.Read())
                    {
                        temp.Add(new Country(reader.GetString(0), reader.GetString(1), reader.GetInt32(2)));
                    }

                return temp;
            }
            set { }
        }
    }

错误是

An unhandled exception occurred while processing the request.
InvalidCastException: Column is null
Npgsql.NpgsqlDataReader.GetFieldValue<T>(int ordinal)

Stack Query Cookies Headers Routing
InvalidCastException: Column is null
Npgsql.NpgsqlDataReader.GetFieldValue<T>(int ordinal)
Npgsql.NpgsqlDataReader.GetString(int ordinal)
dblabb3mvccore.Data.DataAccess.get_GetCountries() in DataAccess.cs
+
                        temp.Add(new Country(reader.GetString(0), reader.GetString(1), reader.GetInt32(2)));
dblabb3mvccore.Controllers.HomeController.Countries() in HomeController.cs
+
            return View(DataAccess.GetCountries);
lambda_method(Closure , object , object[] )
Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(object target, object[] parameters)
Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor+SyncActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, object controller, object[] arguments)
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeActionMethodAsync()
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeNextActionFilterAsync()
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync()
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|24_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, object state, bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync()
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

尽管数据库中没有任何列为空。

但是,如果我将 SQL 命令更改为例如 SELECT name, capital, population FROM country limit 20

我的Country.cs:

    public class Country
    {
        public Country(string name, string capital, int population)
        {
            Name = name;
            Capital = capital;
            Population = population;
        }

        public string Name { get; set; }
        public string Capital { get; set; }
        public int Population { get; set; }
    }

Countries.cshtml:

@model List<Country>
@{
    ViewData["Title"] = "Countries";
}
<h1>@ViewData["Title"]</h1>

<table class="table table-bordered table-striped">
    <thead>
    <tr>
        <th>Name</th>
        <th>Capital</th>
        <th>Population</th>
    </tr>
    </thead>
    <tbody>
    @foreach (var country in Model)
    {
        <tr>
            <td>@country.Name</td>
            <td>@country.Capital</td>
            <td>@country.Population</td>
        </tr>
    }
    </tbody>
</table>

不可否认,我的数据库中有空值。