简体   繁体   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);

        
    }

My Classes我的课程

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;

   
}

Error i am Facing我面临的错误

ystem.InvalidCastException: Unable to cast object of type 'System.DBNull' to type 'System.String'. ystem.InvalidCastException:无法将类型为“System.DBNull”的 object 转换为类型“System.String”。 at MySqlConnector.Core.Row.GetString(Int32 ordinal) in / /src/MySqlConnector/Core/Row.cs:line 377 at MySqlConnector.MySqlDataReader.GetString(Int32 ordinal) in / /src/MySqlConnector/MySqlDataReader.cs:line 287 at lambda_method13(Closure, QueryContext, DbDataReader, ResultContext, SingleQueryResultCoordinator ) at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable 1.AsyncEnumerator.MoveNextAsync() at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ToListAsync[TSource](IQueryable 1 source, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ToListAsync[TSource](IQueryable`1 source, CancellationToken cancellationToken) at WoocommerceApi.Controllers.wp_postsController.Get() in D:\ApiDevelopment\WoocommerceApi\WoocommerceApi\Controllers\wp_postsController.cs:line 24 at lambda_method5(Closure, Object ) at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.TaskOfActionResultExecutor.Exec在 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 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 invoker) at Microsoft.AspNetCore.Routing.EndpointMiddleware.g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger) at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context) at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext) at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider) at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(Ht .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 context) tpContext上下文)

All I want is to get product id and title from wp_posts and get stock qty from wp_postmeta ID in wp_post and post_id in wp_postmeta are the same but still, I get this error if I pass a single id it works but I want all products我想要的只是从 wp_posts 获取产品 ID 和标题,并从 wp_postmeta 获取库存数量 wp_post 中的 ID 和 wp_postmeta 中的 post_id 是相同的,但仍然,如果我传递一个有效的 id,我会收到此错误,但我想要所有产品

Solved it by Adding Nullable Type to my model Class通过将可空类型添加到我的 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 Response is 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
  }
]

Turns out meta value was null结果元值是 null

暂无
暂无

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

相关问题 RowDataBound:从dataTable获取值! 无法将“System.DBNull”类型的对象强制转换为“System.String”类型 - RowDataBound : Getting value from dataTable ! Unable to cast object of type 'System.DBNull' to type 'System.String' System.InvalidCastException:无法将类型为System.DBNull的对象转换为类型为System.String的对象 - System.InvalidCastException: Unable to cast object of type 'System.DBNull' to type 'System.String' 转换:无法将“System.DBNull”类型的对象转换为“System.String”类型 - Transformation: Unable to cast object of type 'System.DBNull' to type 'System.String' 无法将“System.DBNull”类型的对象转换为 Scaffold-DbContext .Net Core 中的“System.String”类型 - Unable to cast object of type 'System.DBNull' to type 'System.String' in Scaffold-DbContext .Net Core Entity Framework Group BY 导致无法将“System.DBNull”类型的对象转换为“System.String”类型 - Entity Framework Group BY causing Unable to cast object of type 'System.DBNull' to type 'System.String' 如何修复无法将类型为“ System.DBNull”的对象转换为类型为“ System.String”的对象 - How can i fix Unable to cast object of type 'System.DBNull' to type 'System.String` 未以根身份连接时,无法将类型为“ System.DBNull”的对象转换为类型为“ System.String”的对象 - Unable to cast object of type 'System.DBNull' to type 'System.String' WHEN NOT CONNECTING AS ROOT 无法将“System.DBNull”类型的 object 转换为类型“System.String” - Unable to cast object of type 'System.DBNull' to type 'System.String` 如果默认情况下字符串为空,为什么我无法将类型为“ System.DBNull”的对象转换为类型为“ System.String”的对象? - If strings are nullable by default, why do i get Unable to cast object of type 'System.DBNull' to type 'System.String'.? db查询:一个字段可以是null,另一个不是“无法将'System.DBNull'类型的object转换为'System.String'类型” - db query: one field ok to be null, another not “Unable to cast object of type 'System.DBNull' to type 'System.String'”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM