Jim
Jim

Reputation: 2828

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

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; }  
}  

Upvotes: 7

Views: 10027

Answers (4)

furier
furier

Reputation: 1977

In my case I was using a DesignTimeDbContextFactory which caught me off guard when trying to create migrations. I had correctly configured Program.cs but forgot about the design time factory, here is the code...

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;

namespace YourNamespace;

public class YourDbContextDesignTimeFactory : IDesignTimeDbContextFactory<YourDbContext>
{
    public YourDbContext CreateDbContext(string[] args)
        => new(new DbContextOptionsBuilder<YourDbContext>()
              .UseSqlServer("Server=YourServer;Database=YourDatabase;", 
               x => x.UseNetTopologySuite()).Options);
}

Upvotes: 1

David Thielen
David Thielen

Reputation: 32986

Heads up on this. Doing the following alone won't resolve the issue:

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
    // already configured for ASP.NET Core
    if (!optionsBuilder.IsConfigured)
    {
        optionsBuilder.UseSqlServer("Data Source=(LocalDb)\\MSSQLLocalDB;Initial Catalog=LouisHowe"
            , x => x.UseNetTopologySuite())
            .EnableSensitiveDataLogging();
            //.LogTo(Debug.WriteLine); 
    }
}

You also need to put it in Program.cs:

builder.Services.AddDbContextFactory<TrackingDbContext>(options =>
    options.UseSqlServer(builder.Configuration.GetConnectionString("LouisHoweDb"),
x => x.UseNetTopologySuite()));

Upvotes: 2

Pushpa Raj Dangi
Pushpa Raj Dangi

Reputation: 59

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">

Upvotes: 4

Jim
Jim

Reputation: 2828

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.

Upvotes: 18

Related Questions