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

InvalidCastException on entities referencing Owned types containing NetTopologySuite Point properties #23282

Closed
jordanwallwork opened this issue Nov 12, 2020 · 3 comments
Assignees
Labels
area-query closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported regression Servicing-approved type-bug
Milestone

Comments

@jordanwallwork
Copy link

jordanwallwork commented Nov 12, 2020

Since upgrading to 5.0.0 I'm finding that attempts to load entities referencing Owned types containing NetTopologySuite Point properties fails. This worked before; the only change to the project has been to retarget to .NET 5.0 and upgrade packages to 5.0.0. Aside from this issue, my project builds and runs just fine.

Steps to Reproduce

I've created a barebones project here which demonstrates the issue. I used the ASP.NET Core Web Application project template, upgraded it to .NET 5.0 and added Microsoft.EntityFrameworkCore.SqlServer.NetTopologySuite 5.0.0 and a couple of simple enties - a Location, which owns an Address which has a Point property.

Stack Trace

System.InvalidCastException: Unable to cast object of type 'Microsoft.EntityFrameworkCore.Query.Internal.BufferedDataReader' to type 'Microsoft.Data.SqlClient.SqlDataReader'.
   at lambda_method11(Closure , QueryContext , DbDataReader , ResultContext , SingleQueryResultCoordinator )
   at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.Enumerator.MoveNext()
   at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query)
   at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression)
   at System.Linq.Queryable.FirstOrDefault[TSource](IQueryable`1 source, Expression`1 predicate)
   at EfErrorTest.Startup.Configure(IApplicationBuilder app, IWebHostEnvironment env) in C:\Projects\EfErrorTest\EfErrorTest\Startup.cs:line 35
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at Microsoft.AspNetCore.Hosting.ConfigureBuilder.Invoke(Object instance, IApplicationBuilder builder)
   at Microsoft.AspNetCore.Hosting.ConfigureBuilder.<>c__DisplayClass4_0.<Build>b__0(IApplicationBuilder builder)
   at Microsoft.AspNetCore.Hosting.GenericWebHostBuilder.<>c__DisplayClass15_0.<UseStartup>b__1(IApplicationBuilder app)
   at Microsoft.AspNetCore.Mvc.Filters.MiddlewareFilterBuilderStartupFilter.<>c__DisplayClass0_0.<Configure>g__MiddlewareFilterBuilder|0(IApplicationBuilder builder)
   at Microsoft.AspNetCore.Server.IIS.Core.IISServerSetupFilter.<>c__DisplayClass2_0.<Configure>b__0(IApplicationBuilder app)
   at Microsoft.AspNetCore.HostFilteringStartupFilter.<>c__DisplayClass0_0.<Configure>b__0(IApplicationBuilder app)
   at Microsoft.AspNetCore.Hosting.GenericWebHostService.StartAsync(CancellationToken cancellationToken)
   at Microsoft.Extensions.Hosting.Internal.Host.StartAsync(CancellationToken cancellationToken)
   at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost host, CancellationToken token)
   at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost host, CancellationToken token)
   at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.Run(IHost host)
   at EfErrorTest.Program.Main(String[] args) in C:\Projects\EfErrorTest\EfErrorTest\Program.cs:line 16

Output

fail: Microsoft.EntityFrameworkCore.Query[10100]
      An exception occurred while iterating over the results of a query for context type 'EfErrorTest.AppDbContext'.
      System.InvalidCastException: Unable to cast object of type 'Microsoft.EntityFrameworkCore.Query.Internal.BufferedDataReader' to type 'Microsoft.Data.SqlClient.SqlDataReader'.
         at lambda_method11(Closure , QueryContext , DbDataReader , ResultContext , SingleQueryResultCoordinator )
         at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.Enumerator.MoveNext()
      System.InvalidCastException: Unable to cast object of type 'Microsoft.EntityFrameworkCore.Query.Internal.BufferedDataReader' to type 'Microsoft.Data.SqlClient.SqlDataReader'.
         at lambda_method11(Closure , QueryContext , DbDataReader , ResultContext , SingleQueryResultCoordinator )
         at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.Enumerator.MoveNext()
crit: Microsoft.AspNetCore.Hosting.Diagnostics[6]
      Application startup exception
      System.InvalidCastException: Unable to cast object of type 'Microsoft.EntityFrameworkCore.Query.Internal.BufferedDataReader' to type 'Microsoft.Data.SqlClient.SqlDataReader'.
         at lambda_method11(Closure , QueryContext , DbDataReader , ResultContext , SingleQueryResultCoordinator )
         at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.Enumerator.MoveNext()
         at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source)
         at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query)
         at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression)
         at System.Linq.Queryable.FirstOrDefault[TSource](IQueryable`1 source, Expression`1 predicate)
         at EfErrorTest.Startup.Configure(IApplicationBuilder app, IWebHostEnvironment env) in C:\Projects\EfErrorTest\EfErrorTest\Startup.cs:line 35
         at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
         at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
         at Microsoft.AspNetCore.Hosting.ConfigureBuilder.Invoke(Object instance, IApplicationBuilder builder)
         at Microsoft.AspNetCore.Hosting.ConfigureBuilder.<>c__DisplayClass4_0.<Build>b__0(IApplicationBuilder builder)
         at Microsoft.AspNetCore.Hosting.GenericWebHostBuilder.<>c__DisplayClass15_0.<UseStartup>b__1(IApplicationBuilder app)
         at Microsoft.AspNetCore.Mvc.Filters.MiddlewareFilterBuilderStartupFilter.<>c__DisplayClass0_0.<Configure>g__MiddlewareFilterBuilder|0(IApplicationBuilder builder)
         at Microsoft.AspNetCore.Server.IIS.Core.IISServerSetupFilter.<>c__DisplayClass2_0.<Configure>b__0(IApplicationBuilder app)
         at Microsoft.AspNetCore.HostFilteringStartupFilter.<>c__DisplayClass0_0.<Configure>b__0(IApplicationBuilder app)
         at Microsoft.AspNetCore.Hosting.GenericWebHostService.StartAsync(CancellationToken cancellationToken)

Provider + Version Info

EF Core version: 5.0.0
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 5.0
Operating system: Windows 10
IDE: Visual Studio 2019 16.8

@jordanwallwork jordanwallwork changed the title Unable to load entities since upgrading to 5.0.0 (InvalidCastException on entities referencing Owned types containing Point properties) Unable to load entities since upgrading to 5.0.0 (InvalidCastException on entities referencing Owned types containing NetTopologySuite Point properties) Nov 12, 2020
@ajcvickers
Copy link
Member

@smitpatel @bricelam I think we've seen this or a similar issue with BufferedDataReader before.

@smitpatel
Copy link
Member

Was different error that result value wasn't cast properly

@ajcvickers ajcvickers added this to the 5.0.1 milestone Nov 12, 2020
@smitpatel smitpatel added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Nov 12, 2020
smitpatel added a commit that referenced this issue Nov 12, 2020
Resolves #23282
Resolves #23104

Issue:
When buffering is enabled, first time when we read particular property slot, we create expression to read into buffer and out of the buffer.
We mistakenly bypassed changing expression to read from value buffer when same property slot was being read again, causing cast errors
Also matches the type being read out of the buffered data reader
smitpatel added a commit that referenced this issue Nov 17, 2020
Resolves #23282
Resolves #23104

Issue:
When buffering is enabled, first time when we read particular property slot, we create expression to read into buffer and out of the buffer.
We mistakenly bypassed changing expression to read from value buffer when same property slot was being read again, causing cast errors
Also matches the type being read out of the buffered data reader
@ajcvickers ajcvickers changed the title Unable to load entities since upgrading to 5.0.0 (InvalidCastException on entities referencing Owned types containing NetTopologySuite Point properties) InvalidCastException on entities referencing Owned types containing NetTopologySuite Point properties Dec 8, 2020
@coremicroservices
Copy link

coremicroservices commented Feb 20, 2021

I'm also getting the same error, Don't know what is going on
"System.InvalidCastException: Unable to cast object of type 'System.String' to type 'System.Int32'.\r\n at Microsoft.Data.SqlClient.SqlBuffer.get_Int32()\r\n at Microsoft.Data.SqlClient.SqlDataReader.GetInt32(Int32 i)\r\n at lambda_method(Closure , QueryContext , DbDataReader , ResultContext , SingleQueryResultCoordinator )\r\n at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable1.AsyncEnumerator.MoveNextAsync()\r\n at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ToListAsync[TSource](IQueryable1 source, CancellationToken cancellationToken)\r\n at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ToListAsync[TSource](IQueryable`1 source, CancellationToken cancellationToken)\r\n

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-query closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported regression Servicing-approved type-bug
Projects
None yet
Development

No branches or pull requests

4 participants