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: "Unable to cast object of type" in many stored procs mapping cases #28803

Closed
ajcvickers opened this issue Aug 20, 2022 · 5 comments · Fixed by #28850 or #28942
Closed

Exception: "Unable to cast object of type" in many stored procs mapping cases #28803

ajcvickers opened this issue Aug 20, 2022 · 5 comments · Fixed by #28850 or #28942
Assignees
Labels
area-save-changes closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Milestone

Comments

@ajcvickers
Copy link
Member

ajcvickers commented Aug 20, 2022

Start with sample here: dotnet/EntityFramework.Docs#3989

Seems to happen a lot when a batch contains multiple updates, but also in other cases. For example, remove the first call to SaveChanges in SprocMappingTest

      Executed DbCommand (11ms) [Parameters=[@p0='2', @p1='3', @p2='1', @p3='1', @p4='2', @p5='2', @p6='2', @p7='3', @p8='3', @p9='2', @p10='2', @p11='0x00000000000007D2' (Size = 8), @p12='Java Puzzlers (New Edition!)' (Nullable = false) (Size = 4000), @p13='283', @p14='2005-01-01T00:00:00.0000000', @p15=NULL (
Size = 8000) (DbType = Binary), @p16='2022-08-20T20:35:53.5200000', @p17='0-321-33678-X' (Size = 4000), @p18=NULL (Nullable = false) (Direction = Output) (DbType = DateTime2), @p19=NULL (Size = 8) (Direction = Output) (DbType = Binary), @p20='3', @p21='0x00000000000007D3' (Size = 8), @p22='Effective Java (New E
dition!)' (Nullable = false) (Size = 4000), @p23='252', @p24='2001-01-01T00:00:00.0000000', @p25='0x010203040506070809' (Size = 8000), @p26='2022-08-20T20:35:53.5200000', @p27='0-201-31005-8' (Size = 4000), @p28=NULL (Nullable = false) (Direction = Output) (DbType = DateTime2), @p29=NULL (Size = 8) (Direction =
 Output) (DbType = Binary), @p30='4', @p31='0x00000000000007D4' (Size = 8), @p32='Test-Driven Development By Example (New Edition!)' (Nullable = false) (Size = 4000), @p33='220', @p34='2003-01-01T00:00:00.0000000', @p35=NULL (Size = 8000) (DbType = Binary), @p36='2022-08-20T20:35:53.5200000', @p37='0-321-14653-
0' (Size = 4000), @p38=NULL (Nullable = false) (Direction = Output) (DbType = DateTime2), @p39=NULL (Size = 8) (Direction = Output) (DbType = Binary), @p40='5', @p41='0x00000000000007D5' (Size = 8), @p42='6', @p43='0x00000000000007D6' (Size = 8), @p44='Amiga Computing' (Nullable = false) (Size = 4000), @p45='90
', @p46='1988-05-16T00:00:00.0000000', @p47=NULL (Size = 8000) (DbType = Binary), @p48='2.95' (Nullable = true) (Precision = 18) (Scale = 2), @p49='1', @p50='4', @p51='2022-08-20T20:35:53.5600000', @p52=NULL (Nullable = false) (Direction = Output) (DbType = DateTime2), @p53=NULL (Size = 8) (Direction = Output)
(DbType = Binary), @p54='1', @p55='0x00000000000007D1' (Size = 8), @p56='2', @p57='3', @p58='2', @p59='Joshua Bloch' (Nullable = false) (Size = 4000), @p60='3', @p61='Neal Gafter' (Nullable = false) (Size = 4000)], CommandType='Text', CommandTimeout='30']
      SET NOCOUNT ON;
      EXEC [Addresses_Delete] @p0;
      EXEC [Addresses_Delete] @p1;
      EXEC [BookPerson_Delete] @p2, @p3;
      EXEC [BookPerson_Delete] @p4, @p5;
      EXEC [BookPerson_Delete] @p6, @p7;
      EXEC [BookPerson_Delete] @p8, @p9;
      EXEC [Book_Update] @p10, @p11, @p12, @p13, @p14, @p15, @p16, @p17, @p18 OUTPUT, @p19 OUTPUT;
      EXEC [Book_Update] @p20, @p21, @p22, @p23, @p24, @p25, @p26, @p27, @p28 OUTPUT, @p29 OUTPUT;
      EXEC [Book_Update] @p30, @p31, @p32, @p33, @p34, @p35, @p36, @p37, @p38 OUTPUT, @p39 OUTPUT;
      EXEC [Magazine_Delete] @p40, @p41;
      EXEC [Magazine_Update] @p42, @p43, @p44, @p45, @p46, @p47, @p48, @p49, @p50, @p51, @p52 OUTPUT, @p53 OUTPUT;
      EXEC [Book_Delete] @p54, @p55;
      EXEC [Contacts_Delete] @p56;
      EXEC [Contacts_Delete] @p57;
      EXEC [Person_Delete] @p58, @p59;
      EXEC [Person_Delete] @p60, @p61;
fail: 8/20/2022 21:35:53.947 CoreEventId.SaveChangesFailed[10000] (Microsoft.EntityFrameworkCore.Update)
      An exception occurred in the database while saving changes for context type 'NewInEfCore7.TpcDocumentsContext'.
      Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while saving the entity changes. See the inner exception for details.
       ---> System.InvalidCastException: Unable to cast object of type 'System.Int32' to type 'System.DateTime'.
         at Microsoft.EntityFrameworkCore.ChangeTracking.ValueComparer`1.Equals(Object left, Object right)
         at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.AreEqual(Object value, Object otherValue, IProperty property)
         at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.SetProperty(IPropertyBase propertyBase, Object value, Boolean isMaterialization, Boolean setModified, Boolean isCascadeDelete, CurrentValueType valueType)
         at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.SetStoreGeneratedValue(IProperty property, Object value)
         at Microsoft.EntityFrameworkCore.Update.ColumnModification.set_Value(Object value)
         at Microsoft.EntityFrameworkCore.Update.ModificationCommand.PropagateOutputParameters(DbParameterCollection parameterCollection, Int32 baseParameterIndex)
         at Microsoft.EntityFrameworkCore.Update.AffectedCountModificationCommandBatch.Consume(RelationalDataReader reader)
         --- End of inner exception stack trace ---
         at Microsoft.EntityFrameworkCore.Update.AffectedCountModificationCommandBatch.Consume(RelationalDataReader reader)
         at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.Execute(IRelationalConnection connection)
         at Microsoft.EntityFrameworkCore.SqlServer.Update.Internal.SqlServerModificationCommandBatch.Execute(IRelationalConnection connection)
         at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.Execute(IEnumerable`1 commandBatches, IRelationalConnection connection)
         at Microsoft.EntityFrameworkCore.Storage.RelationalDatabase.SaveChanges(IList`1 entries)
         at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(IList`1 entriesToSave)
         at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(StateManager stateManager, Boolean acceptAllChangesOnSuccess)
         at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.<>c.<SaveChanges>b__107_0(DbContext _, ValueTuple`2 t)
         at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)
         at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(Boolean acceptAllChangesOnSuccess)
         at Microsoft.EntityFrameworkCore.DbContext.SaveChanges(Boolean acceptAllChangesOnSuccess)
Unhandled exception. Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while saving the entity changes. See the inner exception for details.
 ---> System.InvalidCastException: Unable to cast object of type 'System.Int32' to type 'System.DateTime'.
   at Microsoft.EntityFrameworkCore.ChangeTracking.ValueComparer`1.Equals(Object left, Object right)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.AreEqual(Object value, Object otherValue, IProperty property)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.SetProperty(IPropertyBase propertyBase, Object value, Boolean isMaterialization, Boolean setModified, Boolean isCascadeDelete, CurrentValueType valueType)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.SetStoreGeneratedValue(IProperty property, Object value)
   at Microsoft.EntityFrameworkCore.Update.ColumnModification.set_Value(Object value)
   at Microsoft.EntityFrameworkCore.Update.ModificationCommand.PropagateOutputParameters(DbParameterCollection parameterCollection, Int32 baseParameterIndex)
   at Microsoft.EntityFrameworkCore.Update.AffectedCountModificationCommandBatch.Consume(RelationalDataReader reader)
   --- End of inner exception stack trace ---
   at Microsoft.EntityFrameworkCore.Update.AffectedCountModificationCommandBatch.Consume(RelationalDataReader reader)
   at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.Execute(IRelationalConnection connection)
   at Microsoft.EntityFrameworkCore.SqlServer.Update.Internal.SqlServerModificationCommandBatch.Execute(IRelationalConnection connection)
   at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.Execute(IEnumerable`1 commandBatches, IRelationalConnection connection)
   at Microsoft.EntityFrameworkCore.Storage.RelationalDatabase.SaveChanges(IList`1 entries)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(IList`1 entriesToSave)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(StateManager stateManager, Boolean acceptAllChangesOnSuccess)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.<>c.<SaveChanges>b__107_0(DbContext _, ValueTuple`2 t)
   at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(Boolean acceptAllChangesOnSuccess)
   at Microsoft.EntityFrameworkCore.DbContext.SaveChanges(Boolean acceptAllChangesOnSuccess)
   at Microsoft.EntityFrameworkCore.DbContext.SaveChanges()
   at NewInEfCore7.StoredProcedureMappingSample.SprocMappingTest[TContext]() in C:\github\EntityFramework.Docs\samples\core\Miscellaneous\NewInEFCore7\StoredProcedureMappingSample.cs:line 65
   at NewInEfCore7.StoredProcedureMappingSample.Inset_Update_and_Delete_using_stored_procedures_with_TPC() in C:\github\EntityFramework.Docs\samples\core\Miscellaneous\NewInEFCore7\StoredProcedureMappingSample.cs:line 26
   at Program.Main() in C:\github\EntityFramework.Docs\samples\core\Miscellaneous\NewInEFCore7\Program.cs:line 27
@ajcvickers
Copy link
Member Author

@roji Can we get this fixed ASAP to unblock the stored-procs sample? Fix should target rc2.

@roji
Copy link
Member

roji commented Aug 22, 2022

Affirmative, I'll look at this tomorrow.

roji added a commit to roji/efcore that referenced this issue Aug 23, 2022
roji added a commit to roji/efcore that referenced this issue Aug 23, 2022
roji added a commit to roji/efcore that referenced this issue Aug 23, 2022
@roji roji added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Aug 23, 2022
@smitpatel smitpatel modified the milestones: 7.0.0, 7.0.0-rc2 Aug 29, 2022
@ajcvickers
Copy link
Member Author

Still failing for two of the cases in dotnet/EntityFramework.Docs#4007

@ajcvickers ajcvickers reopened this Aug 31, 2022
@ajcvickers ajcvickers removed this from the 7.0.0-rc2 milestone Aug 31, 2022
@ajcvickers ajcvickers removed the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Aug 31, 2022
@ajcvickers ajcvickers added this to the 7.0.0 milestone Aug 31, 2022
roji added a commit to roji/efcore that referenced this issue Aug 31, 2022
roji added a commit to roji/efcore that referenced this issue Aug 31, 2022
* Don't attempt to propagate rows affected result columns

Fixes dotnet#28803
@roji roji mentioned this issue Aug 31, 2022
@roji
Copy link
Member

roji commented Aug 31, 2022

Looked into this. There are various issues with the sample (dotnet/EntityFramework.Docs#4007 (review)), but also two issues in the update pipeline:

  1. We erroneously attempt to propagate rows affected result columns when another parameter/result column should legitimately be propagated. Sproc fixes #28942 fixes that (@ajcvickers you probably want to run against that PR)
  2. We currently have a debug assertion that there are no unexpected trailing result sets after we've consumed the expected ones. Although that's a user error (it's their responsibility to tell us exactly which sprocs return resultsets etc.), we can help users find out about the mis-configuration by issuing a warning here (@AndriySvyryd makes sense?)

@AndriySvyryd
Copy link
Member

we can help users find out about the mis-configuration by issuing a warning here

Yes, and we should consume them nevertheless

roji added a commit to roji/efcore that referenced this issue Sep 1, 2022
* Don't attempt to propagate rows affected result columns.
* Warn if an unexpected trailing result set is found.
* Throw an informative message if a result set is missing.

Fixes dotnet#28803
@roji roji closed this as completed in d7d6d91 Sep 1, 2022
@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 Sep 9, 2022
@ajcvickers ajcvickers modified the milestones: 7.0.0, 7.0.0-rc2 Sep 9, 2022
@ajcvickers ajcvickers modified the milestones: 7.0.0-rc2, 7.0.0 Nov 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-save-changes closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants