[英]How to use spatial type with Entity Framework code-first without referencing Entity Framework
I have an entity with some spatial data... I am using DbGeography
and DbGeometry
referenced in the System.Data.Entity
assembly... 我有一个带有一些空间数据的实体......我正在使用
System.Data.Entity
程序DbGeometry
引用的DbGeography
和DbGeometry
...
I am not using that one referenced in EntityFramework.dll
because I have a layered solution and I do not want to reference Entity Framework everywhere in my solution, but just inside the DAL... 我没有使用
EntityFramework.dll
引用的那个,因为我有一个分层解决方案,我不想在我的解决方案中到处引用实体框架,但只是在DAL内...
When I try to add a new migration I get the following error: 当我尝试添加新的迁移时,我收到以下错误:
EntityType 'DbGeometry' has no key defined.
EntityType'DbGeometry'没有定义键。 Define the key for this EntityType.
定义此EntityType的键。
So I tried to replace the DbGeometry
property with a string OnModelCreating
I have tried to write the following code: 所以我尝试用字符串
OnModelCreating
替换DbGeometry
属性我试图编写以下代码:
modelBuilder.Entity<AddressInfo>()
.Property(s => s.GeometryLocation)
.HasColumnType("geometry");
But In this case I receive the error that the String data type is not compatible with the DbGeometry
data type... 但在这种情况下,我收到String数据类型与
DbGeometry
数据类型不兼容的DbGeometry
...
Does anyone have any idea to solve this problem? 有没有人有任何想法来解决这个问题? Does anyone know which data type is compatible with
DbGeometry
data type? 有谁知道哪种数据类型与
DbGeometry
数据类型兼容?
Thank you 谢谢
UPDATE UPDATE
EntityType 'DbGeography' has no key defined does not solve my problem.. At the end, the author of the post says that EntityType'DbGeography'没有定义键也没有解决我的问题。最后,帖子的作者说
[He] literally had to add a reference to Entity Framework in [his] model
[他]确实必须在[他的]模型中添加对实体框架的引用
This is exactly what I want to avoid... 这正是我想要避免的......
Ryan also says that 瑞恩也说过
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
我认为它可以通过一个聪明的数据配置图来修复,我会玩它并且如果我遇到任何可行的技术将会更新
Any idea how to fix it? 知道怎么解决吗? Which could be a compatible data type to replace DbGeometry?
哪个可能是兼容的数据类型来取代DbGeometry?
If you are using EF5 you should use DbGeography
and DbGeometry
referenced in System.Data.Spatial
. 如果您使用的是EF5,则应使用
System.Data.Spatial
引用的DbGeography
和DbGeometry
。
But if you are using EF6 you should use DbGeography
and DbGeometry
referenced in System.Data.Entity.Spatial
. 但是如果您使用的是
DbGeography
,则应使用System.Data.Entity.Spatial
引用的DbGeography
和DbGeometry
。
Somebody might come along and give an answer (and I really hope someone does), but as far as I know, this is just not possible. 有人可能会出现并给出答案(我真的希望有人这样做),但据我所知,这是不可能的。 I have experienced with this myself for quite some time, and I haven't come up with a good, usable solution.
我已经经历了很长一段时间,并且我没有找到一个好的,可用的解决方案。 You have to add a reference to EntityFramework.dll if you want to use DbGeography, no matter what (don't know about DbGeometry, though).
如果你想使用DbGeography,你必须添加对EntityFramework.dll的引用,不管是什么(不知道DbGeometry)。
Here's the documentation: 这是文档:
https://msdn.microsoft.com/en-us/library/system.data.entity.spatial.dbgeography(v=vs.113).aspx https://msdn.microsoft.com/en-us/library/system.data.entity.spatial.dbgeography(v=vs.113).aspx
This states that it is in the EntityFramework.dll. 这表明它在EntityFramework.dll中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.