我发现在 SQL Server 中存储 lat 和 long 的最佳类型是十进制 (9,6) (参考。在 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 [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
谢谢