question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

EF Core with spatial column throws exception: Could not load file or assembly Microsoft.SqlServer.Types

See original GitHub issue

My code to reproduce the bug

EFCore5Geography.zip

Bug descrption

I have a small db.sql file in the project which creates a minimal database with one table and one table value function. The table has a geography column and the function makes a query for this table. First question is why the function return type has byte[] for spatial type, why not Geometry like the table parse?

When you run the sample code, the insert is perfect, but when query the function the followin error occures: error I know that Microsoft.SqlServer.Types is a .net framework package only, issued by SQLServer (thats why we use NetTopologySuite), and no .netstandard or core version from it, but who want to use these package? If I query the table with linq, without the function, there is no error.

In the MyDbContext.partial.cs I made a connection with the following settings:

        public MyDbContext(string connectionString, int commandTimeout = 30)
            : base(new DbContextOptionsBuilder<MyDbContext>()
                .UseLazyLoadingProxies()
                .UseSqlServer(
                    connectionString,
                    sqlServerOptions =>
                    {
                        sqlServerOptions.CommandTimeout(commandTimeout);
                        sqlServerOptions.UseNetTopologySuite();
                    })
                .Options)
        {
        }

Include provider and version information

EF Core version: Database provider: (Microsoft.EntityFrameworkCore.SqlServer 5.0.16) Target framework: (.NET 5.0) Operating system: Windows 10 (19041) IDE: (Visual Studio 2022 17.1.3)

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
Dimigergocommented, Apr 19, 2022

Yes, in this case it works perfect.

1reaction
bricelamcommented, Apr 15, 2022

Does it work if you use Geometry (or Point) in the class instead?

  public partial class funcGetCoordinatesResult
  {
-     public byte[] DevCoordinates { get; set; }
+     public Geometry DevCoordinates { get; set; } 
  }

UseNetTopologySuite is designed to seamlessly translate from geography in the database to the NTS types in your model. You shouldn’t ever have to go through WKT, WKB, or the SQL Server serialized bytes yourself.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error while querying for spatial column in the table - Could ...
I am getting an error as "System.IO.FileNotFoundException: 'Could not load file or assembly 'Microsoft.SqlServer.Types, Version=10.0.0.0, ...
Read more >
Could not load file or assembly 'Microsoft.SqlServer.Types ...
And I have set it to Copy Local and can see the .dll in the bin folder on the staging server, however I'm...
Read more >
Microsoft.SqlServer.Types dll not loaded issue with ASP. ...
We have a scenario to load the spatial data from sql in ASP.NET Core and .NET Standard projects. When trying to read the...
Read more >
Solving the "Spatial types and functions are not available ...
This page describes multiple ways to fix the 'Spatial types and functions are not available for this provider because the assembly 'Microsoft.
Read more >
NativeAssemblies loading issues with SqlServerTypes
There is no exception thrown and the loading process is seemingly going through fine. Yet there are no Spatial library loaded in and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found