在一个可移植项目中,我有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项目中都使用了这个模型。在后端项目中,我安装了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。(无法安装实体框架)。
发布于 2018-01-06 20:35:20
这两个类与具有额外Provider属性的实体框架( Entity )几乎完全相同。
您可以使用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
//...
}
}
}
}发布于 2018-03-07 09:51:29
要在名称空间之间切换,我使用扩展:
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);
}
}https://stackoverflow.com/questions/48130057
复制相似问题