1

Error: System.InvalidCastException: Unable to cast object of type 'System.String' to type 'System.Guid'. at Microsoft.Data.SqlClient.SqlBuffer.get_Guid() at Microsoft.Data.SqlClient.SqlDataReader.GetGuid(Int32 i) at lambda_method88(Closure , QueryContext , DbDataReader , ResultContext , SingleQueryResultCoordinator ) at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable1.AsyncEnumerator.MoveNextAsync() at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ToListAsync[TSource](IQueryable1 source, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ToListAsync[TSource](IQueryable1 source, CancellationToken cancellationToken) at AutoMapper.AspNet.OData.QueryableExtensions.GetAsync[TModel,TData](IQueryable1 query, IMapper mapper, Expression1 filter, Expression1 queryFunc, ICollection1 includeProperties, AsyncSettings asyncSettings) at AutoMapper.AspNet.OData.QueryableExtensions.GetAsync[TModel,TData](IQueryable1 query, IMapper mapper, ODataQueryOptions1 options, QuerySettings querySettings) at AutoMapper.AspNet.OData.QueryableExtensions.GetAsync[TModel,TData](IQueryable1 query, IMapper mapper, ODataQueryOptions1 options, HandleNullPropagationOption handleNullPropagation) at TRKAPI.Controllers.RecordsController.GetTaskRecord(ODataQueryOptions1 options) in

Line 80 is cited as cause

_context.TaskRecord.GetAsync(_mapper, options, HandleNullPropagationOption.Default);

This started happening after changing a column in the table from string to uniqueidentifier.

  1. I transfered data to temp table.
  2. I deleted from the table in question.
  3. I altered the column
  4. Running
SELECT DATA_TYPE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'TaskRecord'
  AND COLUMN_NAME = 'TransactionId'

Returns:

DATA_TYPE uniqueidentifier

  1. I then inserted from the temp table, casting the transactionid column into a uniquidentifier
  2. Taking a value from the column and checking it as a valid guid returns valid.
  3. I updated the contract (dto), model and there is no reference to a column in the db context.

I'm not really sure where to look next, ideas?

6
  • Are there any non-schema-bound views involved? Commented Dec 8, 2021 at 20:22
  • 1
    You will need to provide code for the entities involved (TaskRecord, this mentioned Temp table, etc.) and the actual code for the operation you are trying to undertake. For instance if you changed an underlying type in a table or in an entity and not the corresponding location, or you made the change but are attempting to read this updated entity using something like ExecuteSql() from the temp table and the temp table still has a varchar() column, you might get an exception like this. It's anyone's guess without seeing the actual code. Commented Dec 8, 2021 at 20:25
  • @StevePy The code in question is literally just line 80. I made all the changes everywhere else (ef model and the contract (dto) used by the mapper) the column is referenced. The temp table is deleted, I just used the temp table to manipulate the original table. The table reading from is original with valid uniqueidentifier values and is set as a uniqueidentifier type. I literally changed all the respective values from string to Guid in C# and made the column updates in the table. But it looks like it doesn't see it somehow... which is why I'm stumped. Commented Dec 8, 2021 at 20:34
  • @DavidBrowne-Microsoft It is a base table. Commented Dec 8, 2021 at 20:36
  • Well the DataReader has a string column that you are attempting to read as a GUID. Commented Dec 8, 2021 at 20:59

1 Answer 1

1

Well I feel stupid. I had three environments, made the update in one db, but the appsettings was set to another.

Disregard. Sorry.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.