EntityType 'DbGeography' has no key define

2019-03-23 08:00发布

问题:

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?

回答1:

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.