繁体   English   中英

获取所有产品时无法将类型为“System.DBNull”的 object 转换为类型“System.String”

[英]Unable to cast object of type 'System.DBNull' to type 'System.String' On getting all Products

  [HttpGet]
    public async Task<ActionResult> Get()
    {
        var GettingItemDetails = await db.wp_Posts.Join(db.wp_Postmeta,
            post => post.ID,
            meta => meta.post_id,
            (post, meta) => new { Post = post, Meta = meta })
            .Where(x => x.Post.ID > 0)
            .Where(x => x.Post.post_type == "product")
            .Where(x => x.Meta.meta_key == "_stock")
            .Select(x => new
            {
                Post_Title = x.Post.post_title,
                Meta_Key = x.Meta.meta_key,
                Meta_Value = x.Meta.meta_value,
                Product_Id = x.Post.ID,
            }
             ).ToListAsync();
        return Ok(GettingItemDetails);

        
    }

我的课程

public class wp_posts
{
    [Key]
    public int ID { get; set; }
    public int post_author { get; set; } = 0;
    public DateTime post_date { get; set; } = DateTime.Now;
    public DateTime post_date_gmt { get; set; } = DateTime.Now;
    public string post_content { get; set; } = string.Empty;
    public string post_title { get; set; } = string.Empty;
    public string post_excerpt { get; set; } = string.Empty;
    public string post_status { get; set; } = string.Empty;
    public string comment_status { get; set; } = string.Empty;
    public string ping_status { get; set; } = string.Empty;
    public string post_password { get; set; } = string.Empty;
    public string post_name { get; set; } = string.Empty;
    public string to_ping { get; set; } = string.Empty;
    public string pinged { get; set; } = string.Empty;
    public DateTime post_modified { get; set; } = DateTime.Now;
    public DateTime post_modified_gmt { get; set; } = DateTime.Now;
    public string post_content_filtered { get; set; } = string.Empty;
    public int post_parent { get; set; } = 0;
    public string guid { get; set; } = string.Empty;
    public int menu_order { get; set; } = 0;
    public string post_type { get; set; } = string.Empty;
    public string post_mime_type { get; set; } = string.Empty;
    public int comment_count { get; set; } = 0;
    


}

 public class wp_postmeta
{
    [Key]
    public int meta_id { get; set; }
    public int post_id { get; set; } = 0;
    public string meta_key { get; set; } = string.Empty;
    public string meta_value { get; set; } = string.Empty;

   
}

我面临的错误

ystem.InvalidCastException:无法将类型为“System.DBNull”的 object 转换为类型“System.String”。 在 MySqlConnector.Core.Row.GetString(Int32 序号) 在 / /src/MySqlConnector/Core/Row.cs:line 377 在 MySqlConnector.MySqlDataReader.GetString(Int32 ordinal) 在 / /src/MySqlConnector/MySqlDataReader.cs:line 287在 lambda_method13(Closure、QueryContext、DbDataReader、ResultContext、SingleQueryResultCoordinator) 在 Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable 1.AsyncEnumerator.MoveNextAsync() at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ToListAsync[TSource](IQueryable 1 来源,CancellationToken Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ToListAsync[TSource](IQueryable`1 source, CancellationToken cancellationToken) 在 WoocommerceApi.Controllers.wp_postsController.Get() 在 D:\ApiDevelopment\WoocommerceApi\WoocommerceApi\Controllers\wp_postsController.cs: 第 24 行 lambda_method5(关闭,Object)在 Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.TaskOfActionResultExecutor.Exec ute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.g__Logged|12_1(ControllerActionInvoker invoker) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.g__Awaited|13_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted) at Microsoft.AspNetCore .Mvc.Infrastructure.ResourceInvoker.g__Awaited|20_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Logged|17_1(ResourceInvoker invoker) at Microsoft. AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Logged|17_1(ResourceInvoker 调用程序)在 Microsoft.AspNetCore.Routing.EndpointMiddleware.g__AwaitRequestTask|6_0(终结点端点、任务请求任务、ILogger 记录器)在 Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext 上下文) 在 Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext 上下文) 在 Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext) 在 Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpAspNetCore.SwaggerUIMiddleware.Invoke) .Diagnostics.DeveloperExceptionPageMiddleware.Invoke(Ht tpContext上下文)

我想要的只是从 wp_posts 获取产品 ID 和标题,并从 wp_postmeta 获取库存数量 wp_post 中的 ID 和 wp_postmeta 中的 post_id 是相同的,但仍然,如果我传递一个有效的 id,我会收到此错误,但我想要所有产品

通过将可空类型添加到我的 model Class 来解决它

 public class wp_postmeta
{
    [Key]
    public int meta_id { get; set; }
    public int? post_id { get; set; } = 0;
    public string? meta_key { get; set; } = string.Empty;
    public string? meta_value { get; set; } = string.Empty;

   
}

Json 回应是

 [
  {
    "post_Title": "Shirt",
    "meta_Key": "_stock",
    "meta_Value": "20",
    "product_Id": 12
  },
  {
    "post_Title": "Jeans",
    "meta_Key": "_stock",
    "meta_Value": null,
    "product_Id": 13
  }
]

结果元值是 null

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM