Dav*_*ide 12 c# latitude-longitude asp.net-mvc-4 entity-framework-5
我发现在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));
Run Code Online (Sandbox Code Playgroud)
SQL似乎没问题,一切正常,但是当我插入/更新一个值时,即
lat = 44.5912853
Run Code Online (Sandbox Code Playgroud)
它像这样保存:
44.590000
Run Code Online (Sandbox Code Playgroud)
我检查了流程,在更新之前,我的实体包含正确的值,所以我认为不是与我的代码有关,而是与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)
Run Code Online (Sandbox Code Playgroud)

POCO实体
[Table("Breweries")]
public class Brewery : ABrewery
{
....
public decimal Latitude { get; set; }
public decimal Longitude { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
谢谢
显然这个人有完全相同的问题并解决了它:
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);
}
Run Code Online (Sandbox Code Playgroud)
虽然您可能希望在SQL Server 2008及更高版本中使用空间数据类型(特别是geography).
您可以使用DbGeography类型来存储纬度和经度。
using System.Data.Entity.Spatial;
public class Test
{
public DbGeography Location { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6299 次 |
| 最近记录: |