C# EF5,SQL Server,经度和纬度
我发现在SQL Server中存储lat和long的最佳类型是decimal(9,6)(参考文献),我也这样做了C# EF5,SQL Server,经度和纬度,c#,asp.net-mvc-4,entity-framework-5,latitude-longitude,C#,Asp.net Mvc 4,Entity Framework 5,Latitude Longitude,我发现在SQL Server中存储lat和long的最佳类型是decimal(9,6)(参考文献),我也这样做了 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));
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
谢谢两件事:
此外,查看表架构和域实体可能会很有用。将这些字段的数据类型用作Float:
Latitude float,
Longitude float
显然是这样解决的:
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);
}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
SQL Server 2008及更高版本中的modelBuilder.Entity(尤其是)。您可以使用DbGeography类型来存储纬度和经度
using System.Data.Entity.Spatial;
public class Test
{
public DbGeography Location { get; set; }
}
我发布了db表的屏幕截图以及我在Intellitrace中看到的内容(我认为这不是很有用,如果你告诉我如何更好地从EF获取输出,我会发布它)你的C#实体呢?正如你所看到的,我能够分析,在modelbuilder配置中使用HasPrecision方法在updateFix之前已经对值进行了取整。但是无论如何,谢谢:)我看到了“HasPrecision”,但是即使我添加了名称空间,当我尝试使用它时也会出现错误(找不到方法)它是否到达属性
好吗?是的,intellisense都没有找到它。我认为那是一个旧版本,我尝试了不同的方法。但我会再试一次,为什么我没有这个道具。找到它!并解决。我不知道确切的原因,我有一个单独的项目来收集所有EntityTypeConfiguration(这可能没用,但我觉得更有组织性)…在这个项目中没有找到HasPrecision(我将在这里调查并报告原因)…但是如果我在datacontext中设置属性,我可以做到..并解决这个问题。我想说的是,您可能必须将它从原语…
转换为十进制…