You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Npgsql InvalidCastException with Guid Value in Select in Entity Framework Query #3507
Passing a Guid.Empty into a view model property in a projection causes a System.InvalidCastException ("Reading as 'System.Guid' is not supported for fields having DataTypeName 'text'")
var result =
context
.SoftwareVendors
.Select(x => new SoftwareVendorDatatableVm
{
Id = x.Id,
Name = x.Name,
VariableKey = Guid.Empty,
VariableId = Guid.Empty
})
.ToList();
The result:
System.InvalidCastException: Reading as 'System.Guid' is not supported for fields having DataTypeName 'text'
at Npgsql.Internal.AdoSerializerHelpers.g__ThrowReadingNotSupported|0_0(Type type, String displayName, Exception inner)
at Npgsql.Internal.AdoSerializerHelpers.GetTypeInfoForReading(Type type, PostgresType postgresType, PgSerializerOptions options)
at Npgsql.BackendMessages.FieldDescription.g__GetInfoSlow|50_0(Type type, ColumnInfo& lastColumnInfo)
at Npgsql.BackendMessages.FieldDescription.GetInfo(Type type, ColumnInfo& lastColumnInfo)
at Npgsql.NpgsqlDataReader.g__Slow|133_0(ColumnInfo& info, PgConverter& converter, Size& bufferRequirement, Boolean& asObject, <>c__DisplayClass133_0&)
at Npgsql.NpgsqlDataReader.GetFieldValueCore[T](Int32 ordinal)
at lambda_method7389(Closure, QueryContext, DbDataReader, ResultContext, SingleQueryResultCoordinator)
The generated query should probably have looked like this:
Unhandled exception. System.InvalidCastException: Reading as 'System.Guid' is not supported for fields having DataTypeName 'text'
at Npgsql.Internal.AdoSerializerHelpers.<GetTypeInfoForReading>g__ThrowReadingNotSupported|0_0(Type type, PgSerializerOptions options, PgTypeId pgTypeId, Exception inner)
at Npgsql.Internal.AdoSerializerHelpers.GetTypeInfoForReading(Type type, PgTypeId pgTypeId, PgSerializerOptions options)
at Npgsql.BackendMessages.FieldDescription.<GetInfoCore>g__GetInfoSlow|51_0(Type type, ColumnInfo& lastColumnInfo)
at Npgsql.BackendMessages.FieldDescription.GetInfoCore(Type type, ColumnInfo& lastColumnInfo)
at Npgsql.BackendMessages.FieldDescription.GetInfo(Type type, ColumnInfo& lastColumnInfo)
at Npgsql.NpgsqlDataReader.<GetInfo>g__Slow|133_0(ColumnInfo& info, PgConverter& converter, Size& bufferRequirement, Boolean& asObject, <>c__DisplayClass133_0&)
at Npgsql.NpgsqlDataReader.GetFieldValueCore[T](Int32 ordinal)
at Npgsql.NpgsqlDataReader.GetGuid(Int32 ordinal)
at lambda_method33(Closure, QueryContext, DbDataReader, ResultContext, SingleQueryResultCoordinator)
at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.Enumerator.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at Program.<Main>$(String[] args)
@espentveit thanks for filing this and including a minimal repro - I can indeed see the error happening. This isn't PG-specific, so I've opened dotnet/efcore#35826 on the EF side and will close the issue here.
In the meantime, you can workaround the issue by not populating the Guid directly, but by adding another step outside the query to do that.
Activity
espentveit commentedon Mar 21, 2025
A more standalone example:
roji commentedon Mar 21, 2025
@espentveit thanks for filing this and including a minimal repro - I can indeed see the error happening. This isn't PG-specific, so I've opened dotnet/efcore#35826 on the EF side and will close the issue here.
In the meantime, you can workaround the issue by not populating the Guid directly, but by adding another step outside the query to do that.
espentveit commentedon Mar 21, 2025
@roji Sounds good. Thanks.