question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can not project int values from json column

See original GitHub issue

Steps to reproduce

  1. Create ef core model
public class SampleJson
{
    [JsonProperty("int_value")]
    public int IntValue { get; set; }
}

public class EntityWithJson
{
    [Key]
    [Column("id")]
    public int Id { get; set; }

    [Column("json", TypeName = "json")]
    public SampleJson Json { get; set; }
}
  1. Configure DBContext
optionsBuilder.UseMySql(
    "{connection_string}",
    new MySqlServerVersion(new Version(5, 7, 26)),
    x => x.UseNewtonsoftJson(MySqlCommonJsonChangeTrackingOptions.FullHierarchyOptimizedSemantically));
  1. Create projection and try get value from json column
class Projection
{
    public int Value { get; set; }

    public int JsonValue { get; set; }
}

var query = _dbContext.Entities
    .Select(x => new Projection
    {
        Value = x.Id,
        JsonValue = x.Json.IntValue
    });
await query.ToArrayAsync();

The issue

I see exception if i try get value from json

Exception message: Can't convert JSON to Int32
Stack trace:
at MySqlConnector.Core.Row.GetInt32(Int32 ordinal) in /_/src/MySqlConnector/Core/Row.cs:line 218
   at MySqlConnector.MySqlDataReader.GetInt32(Int32 ordinal) in /_/src/MySqlConnector/MySqlDataReader.cs:line 234
   at lambda_method62(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 Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ToArrayAsync[TSource](IQueryable`1 source, CancellationToken cancellationToken)
   at MySqlJsonTest.Tests.Test1() in D:\source\MySqlJsonTest\MySqlJsonTest\UnitTest1.cs:line 39
   at NUnit.Framework.Internal.TaskAwaitAdapter.GenericAdapter`1.BlockUntilCompleted()
   at NUnit.Framework.Internal.MessagePumpStrategy.NoMessagePumpStrategy.WaitForCompletion(AwaitAdapter awaiter)
   at NUnit.Framework.Internal.AsyncToSyncAdapter.Await(Func`1 invoke)
   at NUnit.Framework.Internal.Commands.TestMethodCommand.RunTestMethod(TestExecutionContext context)
   at NUnit.Framework.Internal.Commands.TestMethodCommand.Execute(TestExecutionContext context)
   at NUnit.Framework.Internal.Commands.BeforeAndAfterTestCommand.<>c__DisplayClass1_0.<Execute>b__0()
   at NUnit.Framework.Internal.Commands.BeforeAndAfterTestCommand.RunTestMethodInThreadAbortSafeZone(TestExecutionContext context, Action action)

Further technical details

MySQL version: 8.0 Operating system: Windows 10 x64 Pomelo.EntityFrameworkCore.MySql version: 6.0.1 Microsoft.AspNetCore.App version: 6.0.4

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:12

github_iconTop GitHub Comments

2reactions
lauxjpncommented, May 15, 2022

@denisenko93 Thanks for reporting! You can find the fix in our nightly builds.

1reaction
lauxjpncommented, May 13, 2022

@denisenko93 I’ll test this shorty, but your model should just work, so if it doesn’t in this case, it might be a bug on our end that needs to be fixed.

Read more comments on GitHub >

github_iconTop Results From Across the Web

The JSON value could not be converted to System.Int32
I want to send data of object to my Web API. The API accepts a parameter of class, which properties are type of...
Read more >
Work with JSON data in SQL Server
JSON text is stored in VARCHAR or NVARCHAR columns and is indexed as plain text. Any SQL Server feature or component that supports...
Read more >
Work with JSON data | Cloud Spanner
Create a table with JSON columns. A JSON column can be added to a table when the table is created. JSON type values...
Read more >
MySQL 8.0 Reference Manual :: 11.5 The JSON Data Type
13, a JSON column cannot have a non- NULL default value. Along with the JSON data type, a set of SQL functions is...
Read more >
How To Use JSON in Go
The encoding/json package doesn't just support types like string and int values, though. It can also encode more complex types.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found