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

时间:2018-01-06 17:40:27

标签: asp.net entity-framework xamarin spatial

在便携式项目中,我有Event模型。

    //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项目中使用此模型。 在后端项目中,我安装了Entity Framework 6.没有System.Data.Spatial.DbGeography类,但存在System.Data.Entity.Spatial.DbGeography类。

当我尝试在后端项目中创建新的Event类时,compilator会抛出错误:

  

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

我有哪些选择?

我可以在System.Data.Spatial.DbGeography课程中将System.Data.Entity.Spatial.DbGeography更改为Event但我无法在我的Android项目中使用System.Data.Entity.Spatial.DbGeography。 (无法安装Entity Framework)。

2 个答案:

答案 0 :(得分:0)

两个类几乎与实体框架相同,后者具有额外的Provider属性。

您可以使用Automapper从EF类映射到.NET类。或者您只需在Event课程中复制新属性:

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
                    //...
                }
            }
        }


    }

答案 1 :(得分:0)

在命名空间之间切换我使用扩展名:

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);
    }
}