'EF Core 3.1 The property 'Geometry.UserData' could not be mapped

I have a database-first .net core 3.1 web application which connects to SQL Server database table with a geography column. The database scaffolding and application build completes without any issue however when I run the application I get an error. If I add the [NotMapped] attribute the error is gone but obviously the property is not mapped. What could be the issue?

The property 'Geometry.UserData' could not be mapped, because it is of type 'object' which is not a supported primitive type or a valid entity type. Either explicitly map this property, or ignore it using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'."

I have installed these packages

  • Microsoft.EntityFrameworkCore.SqlServer v3.1.8
  • NetTopologySuite.Core v1.15.3

The scaffolded class look like this

public class MyClass
{  
    public int Id { get; set; }  
    public string Name { get; set; }  
    public NetTopologySuite.Geometries.Geometry Location { get; set; }  
}  


Solution 1:[1]

I have found an answer which is a combination of several things. First, after installing the necessary Topology packages re-run the database ef scaffolding. Then, in Startup.cs add .UseSqlServer(ConnectionString, x=> x.UseNetTopologySuite();. Bear in mind that the generated Datacontext also includes the same line, but it is not excecuted since it is written within if statement.

Solution 2:[2]

After updating NetTopologySuite 5 to 6 it worked for me

Program.cs

builder.Services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection"), sqlOption =>
            sqlOption.UseNetTopologySuite()
         ));




 <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.4" />
  <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer.NetTopologySuite" Version="6.0.4" />
  <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.4">

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jim
Solution 2 Pushpa Raj Dangi