Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EntityType 'DbGeography' has no key defined

Long time listener, first time caller (finally made an account here!)...

I am using Visual Studio 2013 with .NET 4.5.1 and Entity Framework 6 (final releases, not RC or beta).

When trying to add a DbGeography property to my entity, I get this error upon execution:

    One or more validation errors were detected during model generation:
    Geocoder.DbGeography: : EntityType 'DbGeography' has no key defined.
    Define the key for this EntityType.
    DbGeographies: EntityType: EntitySet 'DbGeographies' is based on type 'DbGeography' that has no keys defined.

I have already confirmed I have no references to older versions of Entity Framework (discussed here). I have been using this post and this MSDN article for examples/information as this is my first foray into spatial types in .NET (and SQL Server, for that matter).

Here is my entity:

public class Location
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string City { get; set; }
    public virtual State State { get; private set; }
    public string ZipCode { get; set; }
    public string ZipCodePlus4 { get; set; }
    public DbGeography Geocode { get; set; }
    public Hours Hours { get; set; }
    public virtual ICollection<Language> Languages { get; private set; }
    public virtual OfficeType OfficeType { get; private set; }

    [JsonIgnore]
    public virtual ICollection<ProviderLocation> Providers { get; private set; }
}

What am I doing wrong?

like image 981
Jeff Guillaume Avatar asked Oct 23 '13 19:10

Jeff Guillaume


2 Answers

This turned out to be the opposite of what I read from Microsoft's own response about a similar issue at Codeplex here, and even their documentation here. Did I interpret it wrong? Both of those links indicate that in EF 6, the DbGeography datatype was moved from System.Data.Entity.Spatial to just System.Data.Spatial, but the reverse seems true.

I changed

using System.Data.Spatial;

to

using System.Data.Entity.Spatial;

and it works.

like image 117
Jeff Guillaume Avatar answered Oct 16 '22 13:10

Jeff Guillaume


For anyone else whose might be facing this issue with EF6 (in 2021), try using System.Data.Spatial.DbGeographyWellKnownValue instead of System.Data.Spatial.DbGeography.

This solved my issue. (Not familiar with the intricate details though on how the error is resolved by this.)

like image 45
Jehan Perera Avatar answered Oct 16 '22 12:10

Jehan Perera