SQL DBGeography 第二次插入失败
SQL DBGeography second insert fails
这个很奇怪。我正在尝试通过 MVC 控制器将多边形从 Google 映射保存到 MS SQL。问题是我第一次这样做,它有效,第二次它给了我错误:
The incoming tabular data stream (TDS) remote procedure call (RPC) protocol stream is incorrect. Parameter 3 ("@2"): The supplied value is not a valid instance of data type geography. Check the source data for invalid values. An example of an invalid value is data of numeric type with scale greater than precision.
我正在使用 EntityFramework 6.1.3,先编码。错误出现在下面的提交行中:
var newPoly = new GenericPolygon()
{
Name = webShape.Name,
PolyShape = shapePolygon,
IsEnabled = true,
IsDeleted = false
};
_unitOfWork.PolygonRepository.Add(newPoly);
_unitOfWork.Commit();
SQL table 结构与class 相同,只是它还有一个int ID 标识列,名称为varchar(255)。 PolyShape 列的类型是地理。
shapePolygon变量是这样定义的,class增加了一个只读的属性叫"LongLat",用来从GoogleLatLong切换到 MS LongLat 格式:
var shapePolygon = DbGeography.PolygonFromText("POLYGON((" + webShape.LongLat + "))", 4326);
提交行本身调用数据库上下文保存方法(我使用 UoW 模式来减少代码):
this.context.SaveChanges();
我一辈子都弄不明白为什么它能工作一次,然后就不能再工作,除非我重新启动我的 VS(运行 VS 2013 with IIS Express - SQL 2008 R2服务器上的企业)。
如有任何帮助或指点,我们将不胜感激:-)
我似乎已经缩小了这个问题的范围,虽然它更像是一种解决方法而不是答案,但这可能会对其他人有所帮助。
问题出在SQL服务器的版本上,即SQL2008 R2 10.50.4000。我将我的数据库迁移到 SQL Server 2012 build 11.0.5058,之后代码每次都能正常工作。
希望这对某人有所帮助!
我刚遇到这个问题并通过反转多边形中的点解决了这个问题。显然 SQL 服务器有这些东西什么的。
所以不要像 strGeog += string.Format("{0} {1}, ", latlong[0], latlong[1]) 这样的字符串连接;我将其更改为:
foreach (XmlNode xnPoly in xmlPolyList)
{
strGeog = "";
firstlatlong = null;
if (xnPoly["coordinates"] != null)
{
latlongpairs = xnPoly["coordinates"].InnerText.Replace("\n", "").Split(' ');
foreach (string ll in latlongpairs)
{
latlong = ll.Split(',');
if (firstlatlong == null) firstlatlong = latlong;
strGeog = string.Format("{0} {1}, ", latlong[0], latlong[1]) + strGeog;
}
}
if (strGMPoly.Length > 0)
{
strGeog = strGeog.Substring(0, strGeog.Length - 2); //trim off the last comma and space
strGeog = "POLYGON((" + string.Format("{0} {1} ", firstlatlong[0], firstlatlong[1]) + strGeog + "))"; // conversion from WKT needs it to come back to the first point.
}
i++;
dbPCPoly = new PostCodePolygon();
dbPCPoly.geog = DbGeography.PolygonFromText(strGeog, 4326);
LocDB.PostCodePolygons.Add(dbPCPoly);
LocDB.SaveChanges();
Console.WriteLine(string.Format("Added Polygon {0} for Postcode ({1})", dbPCPoly.PCPolyID, dbPC.PostCodeName));
}
这个很奇怪。我正在尝试通过 MVC 控制器将多边形从 Google 映射保存到 MS SQL。问题是我第一次这样做,它有效,第二次它给了我错误:
The incoming tabular data stream (TDS) remote procedure call (RPC) protocol stream is incorrect. Parameter 3 ("@2"): The supplied value is not a valid instance of data type geography. Check the source data for invalid values. An example of an invalid value is data of numeric type with scale greater than precision.
我正在使用 EntityFramework 6.1.3,先编码。错误出现在下面的提交行中:
var newPoly = new GenericPolygon()
{
Name = webShape.Name,
PolyShape = shapePolygon,
IsEnabled = true,
IsDeleted = false
};
_unitOfWork.PolygonRepository.Add(newPoly);
_unitOfWork.Commit();
SQL table 结构与class 相同,只是它还有一个int ID 标识列,名称为varchar(255)。 PolyShape 列的类型是地理。
shapePolygon变量是这样定义的,class增加了一个只读的属性叫"LongLat",用来从GoogleLatLong切换到 MS LongLat 格式:
var shapePolygon = DbGeography.PolygonFromText("POLYGON((" + webShape.LongLat + "))", 4326);
提交行本身调用数据库上下文保存方法(我使用 UoW 模式来减少代码):
this.context.SaveChanges();
我一辈子都弄不明白为什么它能工作一次,然后就不能再工作,除非我重新启动我的 VS(运行 VS 2013 with IIS Express - SQL 2008 R2服务器上的企业)。
如有任何帮助或指点,我们将不胜感激:-)
我似乎已经缩小了这个问题的范围,虽然它更像是一种解决方法而不是答案,但这可能会对其他人有所帮助。
问题出在SQL服务器的版本上,即SQL2008 R2 10.50.4000。我将我的数据库迁移到 SQL Server 2012 build 11.0.5058,之后代码每次都能正常工作。
希望这对某人有所帮助!
我刚遇到这个问题并通过反转多边形中的点解决了这个问题。显然 SQL 服务器有这些东西什么的。
所以不要像 strGeog += string.Format("{0} {1}, ", latlong[0], latlong[1]) 这样的字符串连接;我将其更改为:
foreach (XmlNode xnPoly in xmlPolyList)
{
strGeog = "";
firstlatlong = null;
if (xnPoly["coordinates"] != null)
{
latlongpairs = xnPoly["coordinates"].InnerText.Replace("\n", "").Split(' ');
foreach (string ll in latlongpairs)
{
latlong = ll.Split(',');
if (firstlatlong == null) firstlatlong = latlong;
strGeog = string.Format("{0} {1}, ", latlong[0], latlong[1]) + strGeog;
}
}
if (strGMPoly.Length > 0)
{
strGeog = strGeog.Substring(0, strGeog.Length - 2); //trim off the last comma and space
strGeog = "POLYGON((" + string.Format("{0} {1} ", firstlatlong[0], firstlatlong[1]) + strGeog + "))"; // conversion from WKT needs it to come back to the first point.
}
i++;
dbPCPoly = new PostCodePolygon();
dbPCPoly.geog = DbGeography.PolygonFromText(strGeog, 4326);
LocDB.PostCodePolygons.Add(dbPCPoly);
LocDB.SaveChanges();
Console.WriteLine(string.Format("Added Polygon {0} for Postcode ({1})", dbPCPoly.PCPolyID, dbPC.PostCodeName));
}