-
Notifications
You must be signed in to change notification settings - Fork 236
Npgsql InvalidCastException with Guid Value in Select in Entity Framework Query #3507
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
Comments
A more standalone example: mkdir guid-test
cd guid-test
dotnet new console
# Replace Program.cs with source code below
dotnet add package Npgsql.EntityFrameworkCore.PostgreSQL
dotnet add package Microsoft.EntityFrameworkCore.Design
dotnet ef migrations add Initial
dotnet ef database update
dotnet run using Microsoft.EntityFrameworkCore;
var context = new BloggingContext();
context.Blogs.Add(new Blog {
BlogId = Guid.NewGuid(),
Url = "/some/url"
});
context.SaveChanges();
var query = context.Blogs.Select(a => new BlogVm {
BlogId = a.BlogId,
Url = a.Url,
SomeViewModelReference = Guid.Empty
}).ToList();
public class BlogVm
{
public Guid BlogId { get; set; }
public string Url { get; set; }
public Guid SomeViewModelReference { get; set; }
}
public class Blog
{
public Guid BlogId { get; set; }
public string Url { get; set; }
}
public class BloggingContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder.UseNpgsql("Server=localhost;port=5432;Database=blog");
}
|
@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. |
@roji Sounds good. Thanks. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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'")
Steps to Reproduce
Running a query using this (simplified):
Gets converted into this:
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:
The text was updated successfully, but these errors were encountered: