Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exception when writing a JSON column with negative enum value #32679

Closed
MoryakSPb opened this issue Dec 27, 2023 · 1 comment
Closed

Exception when writing a JSON column with negative enum value #32679

MoryakSPb opened this issue Dec 27, 2023 · 1 comment
Assignees
Labels
area-type-mapping closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported Servicing-approved type-bug
Milestone

Comments

@MoryakSPb
Copy link

When EF try writing a JSON document that contains negative enum value, an System.OverflowException is thrown.

Test project: https://github.com/MoryakSPb/EfEnumJson

Stack trace:

System.OverflowException: Value was either too large or too small for a UInt64.
   at System.Convert.ThrowUInt64OverflowException()
   at System.Int32.System.IConvertible.ToUInt64(IFormatProvider provider)
   at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
   at Microsoft.EntityFrameworkCore.Storage.Json.JsonWarningEnumReaderWriter`1.ToJsonTyped(Utf8JsonWriter writer, TEnum value)
   at Microsoft.EntityFrameworkCore.Storage.Json.JsonValueReaderWriter`1.ToJson(Utf8JsonWriter writer, Object value)
   at Microsoft.EntityFrameworkCore.Update.ModificationCommand.WriteJson(Utf8JsonWriter writer, Object navigationValue, IUpdateEntry parentEntry, IEntityType entityType, Nullable`1 ordinal, Boolean isCollection, Boolean isTopLevel)
   at Microsoft.EntityFrameworkCore.Update.ModificationCommand.<GenerateColumnModifications>g__HandleJson|41_4(List`1 columnModifications, <>c__DisplayClass41_0&)
   at Microsoft.EntityFrameworkCore.Update.ModificationCommand.GenerateColumnModifications()
   at Microsoft.EntityFrameworkCore.Update.ModificationCommand.<>c.<get_ColumnModifications>b__33_0(ModificationCommand command)
   at Microsoft.EntityFrameworkCore.Internal.NonCapturingLazyInitializer.EnsureInitialized[TParam,TValue](TValue& target, TParam param, Func`2 valueFactory)
   at Microsoft.EntityFrameworkCore.Update.ModificationCommand.get_ColumnModifications()
   at Microsoft.EntityFrameworkCore.Update.UpdateSqlGenerator.AppendInsertReturningOperation(StringBuilder commandStringBuilder, IReadOnlyModificationCommand command, Int32 commandPosition, Boolean& requiresTransaction)
   at Microsoft.EntityFrameworkCore.Sqlite.Update.Internal.SqliteUpdateSqlGenerator.AppendInsertOperation(StringBuilder commandStringBuilder, IReadOnlyModificationCommand command, Int32 commandPosition, Boolean& requiresTransaction)
   at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.AddCommand(IReadOnlyModificationCommand modificationCommand)
   at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.TryAddCommand(IReadOnlyModificationCommand modificationCommand)
   at Microsoft.EntityFrameworkCore.Update.Internal.CommandBatchPreparer.CreateCommandBatches(IEnumerable`1 commandSet, Boolean moreCommandSets, Boolean assertColumnModification, ParameterNameGenerator parameterNameGenerator)+MoveNext()
   at Microsoft.EntityFrameworkCore.Update.Internal.CommandBatchPreparer.BatchCommands(IList`1 entries, IUpdateAdapter updateAdapter)+MoveNext()
   at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.ExecuteAsync(IEnumerable`1 commandBatches, IRelationalConnection connection, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChangesAsync(IList`1 entriesToSave, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChangesAsync(StateManager stateManager, Boolean acceptAllChangesOnSuccess, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.DbContext.SaveChangesAsync(Boolean acceptAllChangesOnSuccess, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.DbContext.SaveChangesAsync(Boolean acceptAllChangesOnSuccess, CancellationToken cancellationToken)
   at EfEnumJson.Tests.EnumTests.EnumToJson(MyEnum value) in I:\SourceCodes\EfEnumJson\EfEnumJson.Tests\EnumTest.cs:line 36

Provider and version information

EF Core version: 8.0.0

Database provider: Microsoft.EntityFrameworkCore.Sqlite
(actual for Npgsql.EntityFrameworkCore.PostgreSQL also)

Target framework: .NET 8.0.0

Operating system: Windows 11 22H2 (22621.2861)

IDE: Visual Studio 2022 17.8.3

MoryakSPb added a commit to MoryakSPb/efcore that referenced this issue Dec 27, 2023
@roji
Copy link
Member

roji commented Dec 27, 2023

/cc @ajcvickers

@ajcvickers ajcvickers self-assigned this Jan 3, 2024
@ajcvickers ajcvickers added this to the 8.0.x milestone Jan 5, 2024
@ajcvickers ajcvickers added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Jan 5, 2024
@ajcvickers ajcvickers modified the milestones: 8.0.x, 8.0.2 Jan 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-type-mapping closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported Servicing-approved type-bug
Projects
None yet
Development

No branches or pull requests

3 participants