Skip to content

DateOnly/TimeOnly with JSON exceptions in preview 4 #30885

@ajcvickers

Description

@ajcvickers

The DateOnly/TimeOnly samples have started failing with Preview 4. Attempting to save the following:

OpeningHours {Id: 1} Unchanged
  Id: 1 PK
  ClosesAt: <null>
  DayOfWeek: 'Sunday'
  OpensAt: <null>
  SchoolId: 1 FK
OpeningHours {Id: 2} Unchanged
  Id: 2 PK
  ClosesAt: '6:00 PM'
  DayOfWeek: 'Monday'
  OpensAt: '8:00 AM'
  SchoolId: 1 FK
OpeningHours {Id: 3} Unchanged
  Id: 3 PK
  ClosesAt: '6:00 PM'
  DayOfWeek: 'Tuesday'
  OpensAt: '8:00 AM'
  SchoolId: 1 FK
OpeningHours {Id: 4} Unchanged
  Id: 4 PK
  ClosesAt: '6:00 PM'
  DayOfWeek: 'Wednesday'
  OpensAt: '8:00 AM'
  SchoolId: 1 FK
OpeningHours {Id: 5} Unchanged
  Id: 5 PK
  ClosesAt: '6:00 PM'
  DayOfWeek: 'Thursday'
  OpensAt: '8:00 AM'
  SchoolId: 1 FK
OpeningHours {Id: 6} Modified
  Id: 6 PK
  ClosesAt: '6:00 PM'
  DayOfWeek: 'Friday'
  OpensAt: '7:00 AM' Modified Originally '8:00 AM'
  SchoolId: 1 FK
OpeningHours {Id: 7} Unchanged
  Id: 7 PK
  ClosesAt: '5:00 PM'
  DayOfWeek: 'Saturday'
  OpensAt: '8:00 AM'
  SchoolId: 1 FK
OpeningHours {Id: 8} Unchanged
  Id: 8 PK
  ClosesAt: <null>
  DayOfWeek: 'Sunday'
  OpensAt: <null>
  SchoolId: 2 FK
OpeningHours {Id: 9} Unchanged
  Id: 9 PK
  ClosesAt: '3:35 PM'
  DayOfWeek: 'Monday'
  OpensAt: '8:45 AM'
  SchoolId: 2 FK
OpeningHours {Id: 10} Unchanged
  Id: 10 PK
  ClosesAt: '3:35 PM'
  DayOfWeek: 'Tuesday'
  OpensAt: '8:45 AM'
  SchoolId: 2 FK
OpeningHours {Id: 11} Unchanged
  Id: 11 PK
  ClosesAt: '3:35 PM'
  DayOfWeek: 'Wednesday'
  OpensAt: '8:45 AM'
  SchoolId: 2 FK
OpeningHours {Id: 12} Unchanged
  Id: 12 PK
  ClosesAt: '3:35 PM'
  DayOfWeek: 'Thursday'
  OpensAt: '8:45 AM'
  SchoolId: 2 FK
OpeningHours {Id: 13} Modified
  Id: 13 PK
  ClosesAt: '12:50 PM'
  DayOfWeek: 'Friday'
  OpensAt: '7:45 AM' Modified Originally '8:45 AM'
  SchoolId: 2 FK
OpeningHours {Id: 14} Unchanged
  Id: 14 PK
  ClosesAt: <null>
  DayOfWeek: 'Saturday'
  OpensAt: <null>
  SchoolId: 2 FK
School {Id: 1} Unchanged
  Id: 1 PK
  Founded: '5/11/1923'
  Name: 'Stowe School'
  OpeningHours: [{Id: 1}, {Id: 2}, {Id: 3}, {Id: 4}, {Id: 5}, {Id: 6}, {Id: 7}]
  Terms: [{Id: 2}, {Id: 1}, {Id: 3}]
School {Id: 2} Unchanged
  Id: 2 PK
  Founded: '5/1/1964'
  Name: 'Farr High School'
  OpeningHours: [{Id: 8}, {Id: 9}, {Id: 10}, {Id: 11}, {Id: 12}, {Id: 13}, {Id: 14}]
  Terms: [{Id: 4}, {Id: 6}, {Id: 5}]
Term {Id: 1} Modified
  Id: 1 PK
  FirstDay: '9/7/2022'
  LastDay: '12/17/2022' Modified Originally '12/16/2022'
  Name: 'Michaelmas'
  SchoolId: 1 FK
  School: {Id: 1}
Term {Id: 2} Unchanged
  Id: 2 PK
  FirstDay: '1/8/2023'
  LastDay: '3/24/2023'
  Name: 'Lent'
  SchoolId: 1 FK
  School: {Id: 1}
Term {Id: 3} Unchanged
  Id: 3 PK
  FirstDay: '4/18/2023'
  LastDay: '7/8/2023'
  Name: 'Summer'
  SchoolId: 1 FK
  School: {Id: 1}
Term {Id: 4} Modified
  Id: 4 PK
  FirstDay: '8/16/2022'
  LastDay: '12/24/2022' Modified Originally '12/23/2022'
  Name: 'Autumn'
  SchoolId: 2 FK
  School: {Id: 2}
Term {Id: 5} Unchanged
  Id: 5 PK
  FirstDay: '1/9/2023'
  LastDay: '3/31/2023'
  Name: 'Winter'
  SchoolId: 2 FK
  School: {Id: 2}
Term {Id: 6} Unchanged
  Id: 6 PK
  FirstDay: '4/17/2023'
  LastDay: '6/29/2023'
  Name: 'Summer'
  SchoolId: 2 FK
  School: {Id: 2}

Generates the following SQL:

fail: 5/14/2023 10:40:48.396 RelationalEventId.CommandError[20102] (Microsoft.EntityFrameworkCore.Database.Command)
      Failed executing DbCommand (3ms) [Parameters=[@p0='07:00' (DbType = String), @p1='1', @p2='07:45' (DbType = String), @p3='2', @p5='1', @p4='12/17/2022' (DbType = Date), @p7='4', @p6='12/24/2022' (DbType = Date)], CommandType='Text', CommandTimeout='30']
      SET NOCOUNT ON;
      UPDATE [Schools] SET [OpeningHours] = JSON_MODIFY([OpeningHours], 'strict $[5].OpensAt', @p0)
      OUTPUT 1
      WHERE [Id] = @p1;
      UPDATE [Schools] SET [OpeningHours] = JSON_MODIFY([OpeningHours], 'strict $[5].OpensAt', @p2)
      OUTPUT 1
      WHERE [Id] = @p3;
      UPDATE [Term] SET [LastDay] = @p4
      OUTPUT 1
      WHERE [Id] = @p5;
      UPDATE [Term] SET [LastDay] = @p6
      OUTPUT 1
      WHERE [Id] = @p7;

Which throws:

Unhandled exception. Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while saving the entity changes. See the inner exception for details.
 ---> System.InvalidCastException: Failed to convert parameter value from a TimeOnly to a String.
 ---> System.InvalidCastException: Object must implement IConvertible.
   at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
   at Microsoft.Data.SqlClient.SqlParameter.CoerceValue(Object value, MetaType destinationType, Boolean& coercedToDataFeed, Boolean& typeChanged, Boolean allowStreaming)
   --- End of inner exception stack trace ---
   at Microsoft.Data.SqlClient.SqlCommand.<>c.<ExecuteDbDataReaderAsync>b__209_0(Task`1 result)
   at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location ---
   at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread)
--- End of stack trace from previous location ---
   at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.ExecuteAsync(IRelationalConnection connection, CancellationToken cancellationToken)
   --- End of inner exception stack trace ---
   at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.ExecuteAsync(IRelationalConnection connection, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.SqlServer.Update.Internal.SqlServerModificationCommandBatch.ExecuteAsync(IRelationalConnection connection, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.ExecuteAsync(IEnumerable`1 commandBatches, IRelationalConnection connection, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.ExecuteAsync(IEnumerable`1 commandBatches, IRelationalConnection connection, CancellationToken cancellationToken)
   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.SqlServer.Storage.Internal.SqlServerExecutionStrategy.ExecuteAsync[TState,TResult](TState state, Func`4 operation, Func`4 verifySucceeded, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.DbContext.SaveChangesAsync(Boolean acceptAllChangesOnSuccess, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.DbContext.SaveChangesAsync(Boolean acceptAllChangesOnSuccess, CancellationToken cancellationToken)
   at NewInEfCore8.DateOnlyTimeOnlySample.DateOnlyTimeOnlyTest[TContext]() in C:\github\EntityFramework.Docs\samples\core\Miscellaneous\NewInEFCore8\DateOnlyTimeOnlySample.cs:line 101
   at NewInEfCore8.DateOnlyTimeOnlySample.DateOnlyTimeOnlyTest[TContext]() in C:\github\EntityFramework.Docs\samples\core\Miscellaneous\NewInEFCore8\DateOnlyTimeOnlySample.cs:line 124
   at Program.Main() in C:\github\EntityFramework.Docs\samples\core\Miscellaneous\NewInEFCore8\Program.cs:line 15
   at Program.<Main>()

Metadata

Metadata

Assignees

Type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions