1

I have an entity which was generated with dotnet ef dbcontext scaffold:

public partial class Company
{
    public DbGeography Location { get; set; }
}

The type in the database is geography. DbGeography is from the System.Data.Entity.Spatial namespace.

When I run the program, I get the following error:

The entity type 'DbGeography' requires a primary key to be defined. If you intended to use a keyless entity type, call 'HasNoKey' in 'OnModelCreating'.

So I add in the context:

modelBuilder.Entity<DbGeography>(builder =>
{
    builder.HasNoKey();
});

Following error occurs:

Unable to determine the relationship represented by navigation 'Company.Location' of type 'DbGeography'. Either manually configure the relationship, or ignore this property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.

Ignoring it in the context is not an option, as I need this field. It results in another error anyway. ('DbGeographyWellKnownValue' requires a primary key to be defined.)

I also don't think I need to add a relationship as DbGeography is not another entity.

How do I proceed from here?

I've already done this 'solution' with no difference: https://stackoverflow.com/a/53693654/7523633

4
  • According to sd.blackball.lv/library/…: "The DbGeometry and DbGeography classes, which have been supported by the classic Entity Framework since version 5.0, cannot yet be used in Entity Framework Core. So far, there is no mapping for the SQL Server Geometry and Geography column types." Is this true?
    – Wouter
    Apr 30, 2021 at 15:44
  • DbGeo is not supported with EF Core
    – ErikEJ
    Apr 30, 2021 at 15:57
  • @ErikEJ, any workarounds from EF Core team, or just do not try that? Apr 30, 2021 at 16:19
  • 1
    Spatial is supported with EF Core and SQL Server, yes.
    – ErikEJ
    Apr 30, 2021 at 19:24

1 Answer 1

1

As per ErikEJ, you can use spacial types. In particular for DbGeography, change the type to 'Point' from the NetTopologySuite.Geometries package. Don't forget to add UseNetTopologySuite.

https://stackoverflow.com/a/53693654/7523633

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.