首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将System.Data.Entity.Spatial.DbGeography转换为System.Data.Spatial.DbGeography

将System.Data.Entity.Spatial.DbGeography转换为System.Data.Spatial.DbGeography
EN

Stack Overflow用户
提问于 2018-01-06 17:40:26
回答 2查看 1K关注 0票数 0

在一个可移植项目中,我有Event模型。

代码语言:javascript
复制
    //Event.cs 
    using System.Data.Spatial;
    public class Event
        {
            public long Id { get; set; }
            public DateTime Date { get; set; }
            public EventType EventType { get; set; }
            [JsonConverter(typeof(DbGeographyConverter))]
            public DbGeography Location { get; set; }
        }

我在后端项目和Android项目中都使用了这个模型。在后端项目中,我安装了EntityFramework6。没有System.Data.Spatial.DbGeography类,但存在System.Data.Entity.Spatial.DbGeography类。

当我试图在后端项目中创建新的事件类时,编译器会抛出错误:

无法将源类型System.Data.Entity.Spatial.DbGeography转换为System.Data.Spatial.DbGeography

我有什么选择?

我可以在我的System.Data.Spatial.DbGeography类中将System.Data.Entity.Spatial.DbGeography更改为System.Data.Entity.Spatial.DbGeography,但是在我的Android项目中不能使用System.Data.Entity.Spatial.DbGeography。(无法安装实体框架)。

EN

回答 2

Stack Overflow用户

发布于 2018-01-06 20:35:20

这两个类与具有额外Provider属性的实体框架( Entity )几乎完全相同。

您可以使用Automapper从EF类映射到.NET类。或者您可以简单地在Event类中复制一个新属性:

代码语言:javascript
复制
using System.Data.Spatial;
public class Event
    {
        public long Id { get; set; }
        public DateTime Date { get; set; }
        public EventType EventType { get; set; }            
        public  System.Data.Entity.Spatial.DbGeography Location { get; set; }
        [JsonConverter(typeof(DbGeographyConverter))]
        public System.Data.Spatial.DbGeography ConvertedLocation 
        { 
            get 
            {
                return new System.Data.Spatial.DbGeography()
                {
                    Area = this.Location.Area,
                    //all other properties except Provider
                    //...
                }
            }
        }


    }
票数 0
EN

Stack Overflow用户

发布于 2018-03-07 09:51:29

要在名称空间之间切换,我使用扩展:

代码语言:javascript
复制
public static class DatabaseExtensions
{
    public static DbGeography ToFrameworkGeography(this System.Data.Spatial.DbGeography geography)
    {
        return DbGeography.FromText(geography.WellKnownValue.WellKnownText, geography.CoordinateSystemId);
    }

    public static DbGeometry ToFrameworkGeometry(this System.Data.Spatial.DbGeometry geometry)
    {
        return DbGeometry.FromText(geometry.WellKnownValue.WellKnownText, geometry.CoordinateSystemId);
    }

    public static System.Data.Spatial.DbGeography ToSystemGeography(this DbGeography geography)
    {
        return System.Data.Spatial.DbGeography.FromText(geography.WellKnownValue.WellKnownText, geography.CoordinateSystemId);
    }

    public static System.Data.Spatial.DbGeometry ToSystemGeometry(this DbGeometry geometry)
    {
        return System.Data.Spatial.DbGeometry.FromText(geometry.WellKnownValue.WellKnownText, geometry.CoordinateSystemId);
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48130057

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档