Can not project int values from json column
See original GitHub issueSteps to reproduce
- 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; }
}
- Configure DBContext
optionsBuilder.UseMySql(
"{connection_string}",
new MySqlServerVersion(new Version(5, 7, 26)),
x => x.UseNewtonsoftJson(MySqlCommonJsonChangeTrackingOptions.FullHierarchyOptimizedSemantically));
- 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:
- Created a year ago
- Comments:12
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@denisenko93 Thanks for reporting! You can find the fix in our nightly builds.
@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.