简体   繁体   中英

DbGeography Alternative for C# POCO

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?

从.NET 4.5开始,他们意识到需要将DbGeography作为核心.NET Framework的一部分,并将其从EntityFramework.dll移到System.Data.Entity.dll中 ,这是他们现在提供的ORM Agnostic API,以便EF任何其他ORM都可以构建它。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM