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

4

7

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; }  
}  
Sniggle answered 5/11, 2020 at 20:23 Comment(8)
You are trying to add Object<T> to database, maybe instead shouldn't you use a foreign key with the Object<T> as its own table?Primipara
Please add table schema into the question.Fin
@janzen the class is scaffolded directly from the database using the ef tool. There is no manual intervention. This is why I am asking the question, since all other tables / properties are scaffolded correctly and can be saved/retrieved from db.Sniggle
@MortezaAsadi the table schema is exactly the same as MyClass with the exception of Location field which type is set to geographySniggle
read this post, maybe helpfulFin
@MortezaAsadi i have seen this post. If you see in the question i am using exactly the same libraries, however i cannot pass this error.Sniggle
Did you add UseNetTopologySuite in the OnConfiguring method?Fin
@MortezaAsadi yes this line is added in the scaffolded datacontext see below (!optionsBuilder.IsConfigured) { optionsBuilder.UseSqlServer(connectionString, x => x.UseNetTopologySuite()); }Sniggle
S
18

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.

Sniggle answered 6/11, 2020 at 16:11 Comment(0)
M
3

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">
Margotmargrave answered 26/4, 2022 at 17:8 Comment(0)
A
2

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()));
Archiplasm answered 17/4, 2023 at 17:23 Comment(2)
Since I set my connection string during the runtime, I actually did need it in the OnConfiguring.Dejong
@Dejong I updated my answer to say put it in both - thanksArchiplasm
G
1

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);
}
Genesia answered 20/3, 2024 at 15:24 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.