25

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?

Community
  • 1
  • 1
Jeff Guillaume
  • 1,661
  • 3
  • 17
  • 29
  • Does your table DbGeography have a primarykey ? – rene Oct 23 '13 at 20:02
  • @rene What do you mean? DbGeography is a built-in data type, as far as I know. In my SQL Server table, the column name is Geocode and the datatype is Geography. – Jeff Guillaume Oct 23 '13 at 20:19
  • Which namespace is the DbGeography in? – rene Oct 23 '13 at 20:26
  • @rene In EF 6, it's in System.Data.Spatial, which is part of EntityFramework.dll. It was moved from System.Data.Entity since EF 5, I think. That's why I mentioned the fact I had confirmed there was no reference to an older EF version in my project. – Jeff Guillaume Oct 23 '13 at 20:35
  • 1
    Did you find [this workitem](http://entityframework.codeplex.com/workitem/650)? It turned out to be a namespace issue. From your code and comment I cannot determine if that workitem is applicable in your case. – rene Oct 23 '13 at 20:39
  • @rene Yes, I had seen that one but it seemed like a duplicate of [this workitem](http://entityframework.codeplex.com/workitem/1535), which implies that you must use System.Data.Spatial, and not System.Data.Entity.Spatial. However, as you'll see in my answer, I did the reverse and now it works. – Jeff Guillaume Oct 23 '13 at 20:50

2 Answers2

33

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.

Jeff Guillaume
  • 1,661
  • 3
  • 17
  • 29
  • 1
    I ran into this same issue when trying to generate my DBContext. When doing this you also have to make sure to rebuild once the using change is made and BEFORE you generate the context. – ferics2 Apr 09 '15 at 18:44
  • That's Incredibruuuuuuuuuuu ! – L.Trabacchin Sep 24 '15 at 15:22
  • 3
    Wait that's not good. That means that a core layer has to hang a dependency on Entity Framework, instead of just System.Data.Entity? As discussed here: http://stackoverflow.com/a/34211927/1507899 – RJB Jun 08 '16 at 05:33
  • 3
    Yeah, good observation RJB, that's a problem. I like to keep my domain model isolated with no references to data implementation specifics. I literally had to add a reference to Entity Framework in my model, ugh. I think it may be fixable with a clever data configuration map though, I'll be playing with it and will update if I run across any workable techniques. – Ryan McArthur Nov 11 '16 at 18:45
  • 1
    Darn, looks like this is planned for a release after EF 7.0 is released, so stuck bringing database implementation into model for now. :( – Ryan McArthur Nov 11 '16 at 20:31
2

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.)