我有一个工具,我写的导入shapefile数据到我们的数据库。它使用EntityFramework 6.1.3和.NET 4.5。
然而,当我将相同的代码移动到现有的web应用程序中,在启动时为数据库设定种子时,它工作得很好,失败的原因是:
“不存在从对象类型Microsoft.SqlServer.Types.SqlGeometry到已知托管提供程序本机类型的映射。”
web应用程序也使用EF 6.1.3和.NET 4.5。包使用NuGet进行管理。我们使用DotSpatial库加载shapefile。
我使用的是System.Data.Entity.Spatial中定义的DbGeometry;
foreach (IFeature feature in sf.Features)
{
var wkt = feature.ToShape().ToGeometry().ToString();
AdminDistrict ad = new AdminDistrict()
{
Id = Convert.ToInt64(feature.DataRow[fieldMap["Id"]]),
ShortName = feature.DataRow[fieldMap["ShortName"]].ToString(),
LongName = feature.DataRow[fieldMap["LongName"]].ToString(),
Type = config.Type,
Country = config.Country,
Timestamp = DateTime.UtcNow,
Geom = DbGeometry.FromText(wkt, 4326)
};
db.AdminDistricts.Add(ad);
}
db.SaveChanges(); //Exception is here...我要创建‘新的’web应用程序项目,并尝试同样的事情,将提供结果时,我有他们。
发布于 2015-07-28 00:03:36
如果在未安装SQL Server的计算机上使用SQL Server Spatial类型,则需要在项目中包含以下NuGet软件包。
https://www.nuget.org/packages/Microsoft.SqlServer.Types
允许您在未安装SQL Server的计算机上使用SQL Server空间类型。在部署到Windows Azure时很有用。还允许使用实体框架空间类型(DbGeography和DbGeometry)。
发布于 2015-07-28 01:45:27
发现一些较旧的库引用导致加载System.Data v2.0.0.0。我添加了一个新的assemblyBinding,以确保将其加载到v4.0
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Data" publicKeyToken="b77a5c561934e089" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.0.0.0" newVersion="4.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>https://stackoverflow.com/questions/31657761
复制相似问题