Closed. This question needs to be more focused。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗?更新问题,使其仅通过editing this post专注于一个问题。
                        
                        2年前关闭。
                                                                                            
                
        
使用DbGeography包中的Microsoft.SqlServer.Type类型使用Entity Framework存储空间数据(正点)的过程在MSSQL DB中显示如下:

0xE6100000010CAA00B9D75CA84740F4FDD478E926FA3F

此十六进制字符串的格式如何,如何将其用于初始化新的DbGeography对象?

最佳答案

编辑

我找到了一种方法,首先将其转换为DbGeography类型SqlGeography,将其转换为SqlGeography

确保添加对以下内容的引用:using Microsoft.SqlServer.Types;

    static void Main(string[] args)
    {
        //0xE6100000010C3D7D04FEF012414034B275BA3D4E5240
        var point = "E6100000010C3D7D04FEF012414034B275BA3D4E5240";

        var pointBytes =  Enumerable.Range(0, point.Length)
                 .Where(x => x % 2 == 0)
                 .Select(x => Convert.ToByte(point.Substring(x, 2), 16))
                 .ToArray();


        var sqlbytes = new SqlBytes(pointBytes);


        var sqlPoint = SqlGeography.Deserialize(sqlbytes);

        Console.WriteLine("SqlGeography = {0} - {1}", sqlPoint.Lat, sqlPoint.Long);


        DbGeography newGeography = DbGeography.FromText(sqlPoint.ToString(), DbGeography.DefaultCoordinateSystemId);

        Console.WriteLine("EF DBGeography = {0} - {1}", newGeography.Latitude, newGeography.Longitude);


        Console.ReadKey();

    }


确认它是正确的:

c# - 使用SQL地理字段初始化.NET DbGeography对象-LMLPHP

09-18 18:36