C#POCO的DbGeography替代方案 [英] DbGeography Alternative for C# POCO

查看:165
本文介绍了C#POCO的DbGeography替代方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个应用程序,我需要在某个位置的半径范围内查询记录。我在PCO上开始只是一个lat / long对的属性,但意识到SQL中的空间搜索是针对一种列类型的地理位置完成的,它们可以转换为POCO中的DbGeography(在另一个SO文档中引用,也使用EF Powertools Reverse工程师POCO)。



所以,我看到的问题是我保持我的POCOS尽可能干净,留下所有引用/依赖关系到实体框架和持久存储尽可能。我有一个模型/域组件中的POCOS,它不应该有任何引用EF。只有我的Repository类和DataStore for DbContext子类和Fluent Configuration项目才知道EF。我也使用流畅的配置远离DataAnnotation属性。一旦你放置DbGeography,你需要使用System.Data.Entity.Spatial和EF来打破持久性的不可知的方法,至少对于PlainOld C#Object >

有了这么多的数据库平台,并使这个系统作为未来的证明尽可能的最小的努力重写数据存储代码在我想切换到另一个持久性存储的事件是非常重要的是保持我的域代码尽可能干净。我发现引入了基于EF流畅代码的配置,允许我们不必使用DataAnnotations属性,因此可以将System.Data.Entity保持在混合状态之外,但是空间上它们会破坏模式。



有谁知道如何处理我想要做的事情?



- Scott发表评论后更新:
所以有还有一个小问题。
我有对System.Data.Entity的引用,我有这个在我的模型:
public System.Data.Spatial.DbGeography GeoLocation {get;组; }

我在配置类中有这个:
this.Property(t => t.GeoLocation).HasColumnName( GeoLocation)HasColumnType(geography);



this.Property被下划线,我得到这个编译错误:
严重性代码描述项目文件行抑制状态
错误CS0453类型DbGeography必须是不可为空的值类型,以便在通用类型或方法StructuralTypeConfiguration.Property(Expression> )'FoodRadar.DataStore C:\Developer\SrcSt\FoodRadar\FoodRadar.DataStore\Configuration\VendorConfiguration.cs 66 Active



我尝试使用一个t4模板的另一个反向poco生成器,它使用System.Data.Entity.Spatial.DbGeography生成,但需要引用EntityFramework。



我应该指定映射?

解决方案

从.NET 4.5起,他们意识到需要将 DbGeography 作为核心.NET Framework的一部分,并将其移动href =https://msdn.microsoft.com/en-us/library/system.data.entity.spatial.dbgeography(v=vs.113).aspx =noreferrer> out of EntityFramework.dll 并进入系统.Data.Entity.dll ,它是现在提供的ORM Agnostic API,以便EF和任何其他ORM可以针对它构建。


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?

解决方案

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.

这篇关于C#POCO的DbGeography替代方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆