我想使用asp .net core v2 web api服务来进行一些空间计算。我认为这是不可能的,因为.net标准2.0中的dbgeography空间类型的Net标准2.0缺乏支持。目前是否有任何解决方法,直到dbgeography或它的等价物被支持为止?
答案 0 :(得分:1)
我想发表评论,但我还不能。尝试查看这篇文章,它显示了使用空间操作的转变:System.Data.Entity.Spatial replacement in ASP.NET Core
答案 1 :(得分:0)
尝试了不同的库之后,我最终完成了自己的课程。我想与大家分享一下,以便对其进行优化。
-场景:需要获取用户与商店之间的距离
-商店类别:
public class Store
{
public int Id { get; set; }
public string Description { get; set; }
public string Name { get; set; }
public Location Location { get; set; }
}
-位置类别:
public class Location
{
public DataContext _dbContext { get; set; }
public Location()
{
}
public Location(DataContext dbContext)
{
_dbContext = dbContext;
}
public double Longitude { get; set; }
public double Latitude { get; set; }
public double Distance(Location destination, int srid=4326)
{
var source = this;
var Distance = string.Empty;
var query = @"DECLARE @target geography = geography::Point(" + destination.Latitude + @"," + destination.Longitude + @"," +srid+@")
DECLARE @orig geography = geography::Point(" + source.Latitude + @"," + source.Longitude + @"," + srid + @")
SELECT @orig.STDistance(@target) as Distance";
try
{
var dbConn = _dbContext.Database.GetDbConnection();
dbConn.Open();
var command = dbConn.CreateCommand();
command.CommandType = System.Data.CommandType.Text;
command.CommandText = query;
return Convert.ToDouble(command.ExecuteScalar().ToString());
}
catch (Exception ex)
{
Error.LogError(ex);
throw ex;
}
}
}
请确保在商店类中的位置属性上添加数据批注[NotMapped]或在数据上下文类中添加以下行:
modelBuilder.Entity<Store>().OwnsOne(c => c.Location);
并像这样使用它
Location loc = new Location(_dbContext); var store = _dbContext.Store.FirstOrDefault(); loc.Longitude = 55.22; loc.Latitude = 33.55; var distance = store.Location.Distance(loc);
如果需要帮助,您可以随时与我联系。