DbGeography Alternative for C# POCO
Asked Answered
T

1

5

I'm writing an application where I need to query for a records within a radius of some position. I started out with just a lat / long pair of properties on my PCO but realised that spatial searches in SQL are done against a column type of geography which translates down to DbGeography in the POCO (referenced in another SO post and also using EF Powertools Reverse Engineer POCO).

So, the problem I'm seeing is that I keep my POCOS as clean as possible leaving out all references / dependencies to entity framework and the persistence store as possible. I have my POCOS in a model/Domain assembly which should never have any references to EF. Only my Repository classes and DataStore for DbContext subclass and Fluent Configuration projects know about EF. I also stay away from DataAnnotation attributes using fluent configuration. As soon as you put DbGeography you need 'using System.Data.Entity.Spatial' and 'EF' which breaks the persistence agnostic approach, at least for the "Plain" Old C# Object.

With so many database platforms around and to make this system as future proof as possible with minimal effort to rewrite the data store code in the event I want to switch to another persistence store it's very important to keep my domain code as clean as possible. I find it odd that EF fluent code based config was introduced and allowed us to not have to use DataAnnotations attributes therefore keeping System.Data.Entity out of the mix, yet with spatial they break the pattern.

Does anyone know how to approach what I'm trying to do?

--Update after Scott's Comment: So there is a slight issue still. I have the reference to System.Data.Entity and I have this on my model: public System.Data.Spatial.DbGeography GeoLocation { get; set; }

I have this in my configuration class: this.Property(t => t.GeoLocation).HasColumnName("GeoLocation").HasColumnType("geography");

The this.Property gets underlined and I get this compile error: Severity Code Description Project File Line Suppression State Error CS0453 The type 'DbGeography' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'StructuralTypeConfiguration.Property(Expression>)' FoodRadar.DataStore C:\Developer\SrcSt\FoodRadar\FoodRadar.DataStore\Configuration\VendorConfiguration.cs 66 Active

I tried another reverse poco generator which uses a t4 template and it generates using System.Data.Entity.Spatial.DbGeography but that requires a reference to EntityFramework still.

How am I supposed to specify the mapping?

Ta answered 10/12, 2015 at 21:8 Comment(0)
I
6

As of .NET 4.5 they realized the need to have DbGeography as part of the core .NET Framework and moved it out of EntityFramework.dll and into System.Data.Entity.dll, which is the ORM Agnostic API they now provide so that EF and any other ORM can build against it.

Inexcusable answered 10/12, 2015 at 21:19 Comment(3)
Awesome thanks. I see now that it's in namespace System.Data.Spatial. Might have to get the EF Powertools devs to updats that.Ta
please go ahead and mark as Answer if that helped you solve your issue.Ghost
@ScottChamberlain I don't think you are right, System.Data.Entity.dll actually predates the Entity Framework 6 (even though MSDN claims that it was introduced in Fx 4.5). In any case I have tried editing the POCO template to point to the System.Data.Spatial.DbGeography to no avail. Not sure why this was marked as an answer, as it stands now Geography type has no place in POCOs due to its hard dependency on EF6 assembly.Trip

© 2022 - 2024 — McMap. All rights reserved.