EF5, SQL Server, Longitude and Latitude

后端 未结 4 899
误落风尘
误落风尘 2021-02-13 17:57

I found that the best type to store lat and long in SQL Server is decimal (9,6) (ref. What datatype to use when storing latitude and longitude data in SQL databases?) and so I d

相关标签:
4条回答
  • 2021-02-13 18:37

    Apparently this guy had the exact same problem and solved it thus:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Activity>().Property(a => a.Latitude).HasPrecision(18, 9);
        modelBuilder.Entity<Activity>().Property(a => a.Longitude).HasPrecision(18, 9);
    }
    

    Although you may want to look at using the spatial data types (particularly geography) in SQL Server 2008 and later.

    0 讨论(0)
  • 2021-02-13 18:42

    You can use DbGeography type for storing Latitude and Longitude.

    using System.Data.Entity.Spatial;
    
    public class Test
    {
        public DbGeography Location { get; set; }
    }
    
    0 讨论(0)
  • 2021-02-13 18:47

    Two things:

    1. I just purchased a Zip code database and it stores all latitude and longitude values as decimal(12,6) data type. I don't think this is going to radically change your results though.

    2. I would check the exact SQL being sent to your SQL Server. You can then check to see where the rounding is occurring. You can check the SQL being sent by grabbing the output from EF or using SQL Profiler. My guess is that it's occuring in your C# code.

    Also, it might be useful to see your table schema and your domain entity.

    0 讨论(0)
  • 2021-02-13 18:51

    Use the data type of these fields as Float:

     Latitude float,
     Longitude float
    
    0 讨论(0)
提交回复
热议问题