微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

无法使用Npgsql.EntityFrameworkCore.PostgreSQL3.1.4查询多行

如何解决无法使用Npgsql.EntityFrameworkCore.PostgreSQL3.1.4查询多行

我在一个.NET Core MVC 3.1项目中,使用Npgsql.EntityFrameWorkCore.Postgresql(3.1.4),并已配置到远程postgresql DB服务器的连接字符串。我没有使用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.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 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 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,population FROM country limit 20,它就可以工作。

我的国家/地区: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>

解决方法

诚然,我的数据库中有空值。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。