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
When including a constant Guid in a projected, unmapped type (e.g. DTO), EF seems to treat it as text and fails loading.
Minimal repro:
awaitusingvarcontext=newBlogContext();awaitcontext.Database.EnsureDeletedAsync();awaitcontext.Database.EnsureCreatedAsync();context.Blogs.Add(newBlog{Id=Guid.NewGuid()});awaitcontext.SaveChangesAsync();varquery=awaitcontext.Blogs.Select(a =>newBlogVm{Id=a.Id,SomeInt=8,// This worksSomeViewModelReference=Guid.Empty,// This fails}).ToListAsync();publicclassBlogContext:DbContext{publicDbSet<Blog>Blogs{get;set;}protectedoverridevoidOnConfiguring(DbContextOptionsBuilderoptionsBuilder)=>optionsBuilder.UseSqlServer("Server=localhost;Database=test;User=SA;Password=Abcd5678;Connect Timeout=60;ConnectRetryCount=0;Encrypt=false").LogTo(Console.WriteLine,LogLevel.Information).EnableSensitiveDataLogging();}publicclassBlog{publicGuidId{get;set;}}publicclassBlogVm{publicGuidId{get;set;}publicGuidSomeViewModelReference{get;set;}publicintSomeInt{get;set;}}
SQL:
SELECT [b].[Id], 8AS [SomeInt], '00000000-0000-0000-0000-000000000000'AS [SomeViewModelReference]
FROM [Blogs] AS [b]
Exception:
Unhandled exception. System.InvalidCastException: Unable to cast object of type 'System.Guid' to type 'System.String'.
at Microsoft.Data.SqlClient.SqlBuffer.get_Guid()
at Microsoft.Data.SqlClient.SqlDataReader.GetGuid(Int32 i)
at lambda_method23(Closure, QueryContext, DbDataReader, ResultContext, SingleQueryResultCoordinator)
at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.AsyncEnumerator.MoveNextAsync() in /Users/roji/projects/efcore/src/EFCore.Relational/Query/Internal/SingleQueryingEnumerable.cs:line 364
Of course, when investigating this check other types as well, as this is likely part of a larger problem.
When including a constant Guid in a projected, unmapped type (e.g. DTO), EF seems to treat it as text and fails loading.
Minimal repro:
SQL:
Exception:
Of course, when investigating this check other types as well, as this is likely part of a larger problem.
Originally reported by @espentveit in npgsql/efcore.pg#3507
The text was updated successfully, but these errors were encountered: