EF5,SQL Server,经度和纬度

我发现在SQL Server中存储lat和long的最佳类型是decimal(9,6)(ref。 在SQL数据库中存储纬度和经度数据时使用什么数据类型? )所以我做了

AddColumn("dbo.Table", "Latitude", c => c.Decimal(nullable: false, precision: 9, scale: 6)); AddColumn("dbo.Table", "Longitude", c => c.Decimal(nullable: false, precision: 9, scale: 6)); 

SQL似乎没问题,一切正常,但是当我插入/更新一个值时,即

 lat = 44.5912853 

它像这样保存:

 44.590000 

我检查了流程,就在更新之前,我的实体包含正确的值,所以我认为不是与我的代码相关,而是与EF / SQL的一些相关。 你有什么想法可以避免这种情况吗?

UPDATE

 update [dbo].[Breweries] set [RankId] = @0, [Name] = @1, [Foundation] = null, [DirectSale] = @2, [OnlineSale] = @3, [StreetAddress] = @4, [StreetAddress1] = null, [ZIP] = @5, [City] = @6, [Province] = @7, [CountryCode] = @8, [Latitude] = @9, [Longitude] = @10, [PIVA] = null, [CodFiscale] = null where ([BreweryId] = @11) 

在此处输入图像描述

POCO实体

 [Table("Breweries")] public class Brewery : ABrewery { .... public decimal Latitude { get; set; } public decimal Longitude { get; set; } } 

SQL事件探查器

 exec sp_executesql N'update [dbo].[Breweries] set [RankId] = @0, [Name] = @1, [Foundation] = null, [DirectSale] = @2, [OnlineSale] = @3, [StreetAddress] = @4, [StreetAddress1] = null, [ZIP] = @5, [City] = @6, [Province] = @7, [CountryCode] = @8, [Latitude] = @9, [Longitude] = @10, [PIVA] = null, [CodFiscale] = null where ([BreweryId] = @11) ',N'@0 int,@1 nvarchar(128),@2 bit,@3 bit,@4 nvarchar(256),@5 varchar(16),@6 nvarchar(64),@7 nvarchar(64),@8 nvarchar(128),@9 decimal(18,2),@10 decimal(18,2),@11 int',@0=2,@1=N'Davide',@2=0,@3=0,@4=N'Via Moscardini, 24',@5='zip',@6=N'city',@7=N'province',@8=N'ITA', @9=44.59,@10=11.05,@11=2 

谢谢

显然这个人有完全相同的问题并解决了它:

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

虽然您可能希望在SQL Server 2008及更高版本中使用空间数据类型 (特别是geography )。

您可以使用DbGeography类型存储纬度和经度。

 using System.Data.Entity.Spatial; public class Test { public DbGeography Location { get; set; } } 

两件事情:

  1. 我刚刚购买了一个邮政编码数据库,它将所有纬度和经度值存储为十进制(12,6)数据类型。 我不认为这会彻底改变你的结果。

  2. 我会检查发送到SQL Server的确切SQL。 然后,您可以检查舍入发生的位置。 您可以通过从EF获取输出或使用SQL事件探查器来检查发送的SQL。 我的猜测是它在你的C#代码中出现了。

此外,查看表架构和域实体可能很有用。

将这些字段的数据类型用作Float:

  Latitude float, Longitude float