Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法将SqlGeography用作Dapper和ASP.NET核心的参数_C#_Sql Server_Asp.net Core_.net Core_Dapper - Fatal编程技术网

C# 无法将SqlGeography用作Dapper和ASP.NET核心的参数

C# 无法将SqlGeography用作Dapper和ASP.NET核心的参数,c#,sql-server,asp.net-core,.net-core,dapper,C#,Sql Server,Asp.net Core,.net Core,Dapper,我正在尝试使用Dapper 1.50.2和ASP.NET Core 2.1在SQL Server中插入一个“地理”数据类型 我在几个线程上读到它应该是这样的,但是在尝试插入数据类型时,我收到了一个异常 注意:我在实体中使用的是非.NET CORE数据类型Microsoft.SqlServer.Types:14.0.1016.290,因为我找不到与.NET核心兼容的良好地理数据类型 实体: public class Address : Entity{ /* .. */ public

我正在尝试使用Dapper 1.50.2和ASP.NET Core 2.1在SQL Server中插入一个“地理”数据类型

我在几个线程上读到它应该是这样的,但是在尝试插入数据类型时,我收到了一个异常

注意:我在实体中使用的是非.NET CORE数据类型
Microsoft.SqlServer.Types:14.0.1016.290
,因为我找不到与.NET核心兼容的良好地理数据类型

实体:

public class Address : Entity{
    /* .. */
    public SqlGeography SpatialLocation { get; set; }
    /* .. */
}
插入方法(标准):

插入方法(专用):

我也尝试过dapper的动态参数方法(从第一篇文章开始),但我不确定如何将它们与不同的参数一起应用

例外情况

The member spatial of type Microsoft.SqlServer.Types.SqlGeography cannot be 
used as a parameter value
更新

我已经用这段代码解决了这个问题。事实证明,正确使用语法很困难

public override Address Insert(Address entity){
    if (entity == null){
        throw new ArgumentNullException(nameof(entity));
    }

    var sql = $"INSERT INTO [dbo].[Address]"
              + "([CreationDate]"
              + ",[StartDate]"
              + ",[EndDate]"
              + ",[CreationUser]"
              + ",[CityId]"
              + ",[Street]"
              + ",[Street2]"
              + ",[SpatialLocation]"
              + ",[Flags])"
              + "VALUES"
              + "(@creationDate"
              + ",@startDate"
              + ",@endDate"
              + ",@creationUser"
              + ",@cityId"
              + ",@street"
              + ",@street2"
              + ",@spatial "
              + ",@flags); "
              + "SELECT SCOPE_IDENTITY()";

    string lat = entity.SpatialLocation.Lat.Value.ToString(CultureInfo.InvariantCulture);
    string longitude = entity.SpatialLocation.Long.Value.ToString(CultureInfo.InvariantCulture);
    entity.Id = DapperExtensionsProxy.ExecuteScalar<int>(sql,new{
                                                             creationDate = entity.CreationDate,
                                                             startDate = entity.StartDate,
                                                             endDate = entity.EndDate,
                                                             creationUser = entity.CreationUser,
                                                             cityId = entity.CityId,
                                                             street = entity.Street,
                                                             street2 = entity.Street2,
                                                             flags = entity.Flags,
                                                             spatial = $"POINT({lat} {longitude} 4326)"
                                                         });
    return entity;
}
公共覆盖地址插入(地址实体){
if(实体==null){
抛出新ArgumentNullException(nameof(entity));
}
var sql=$“插入[dbo].[Address]”
+“([创作日期]”
+“,[起始日期]”
+“,[EndDate]”
+“,[CreationUser]”
+“,[CityId]”
+“,[街]”
+“,[Street2]”
+“,[空间分配]”
+“,[Flags])”
+“价值观”
+“(@creationDate”
+“,@startDate”
+“,@endDate”
+“,@creationUser”
+“,@cityId”
+“,@街”
+“,@street2”
+“,@spatial”
+“,@flags);”
+“选择范围_标识()”;
string lat=entity.spaceAllocation.lat.Value.ToString(CultureInfo.InvariantCulture);
字符串经度=entity.SpatialAllocation.Long.Value.ToString(CultureInfo.InvariantCulture);
entity.Id=DapperExtensionsProxy.ExecuteScalar(sql,新{
creationDate=实体。creationDate,
startDate=entity.startDate,
endDate=entity.endDate,
creationUser=entity.creationUser,
cityId=entity.cityId,
街道=实体。街道,
street2=实体。street2,
标志=实体。标志,
空间=$“点({lat}{longitude}4326)”
});
返回实体;
}
The member spatial of type Microsoft.SqlServer.Types.SqlGeography cannot be 
used as a parameter value
public override Address Insert(Address entity){
    if (entity == null){
        throw new ArgumentNullException(nameof(entity));
    }

    var sql = $"INSERT INTO [dbo].[Address]"
              + "([CreationDate]"
              + ",[StartDate]"
              + ",[EndDate]"
              + ",[CreationUser]"
              + ",[CityId]"
              + ",[Street]"
              + ",[Street2]"
              + ",[SpatialLocation]"
              + ",[Flags])"
              + "VALUES"
              + "(@creationDate"
              + ",@startDate"
              + ",@endDate"
              + ",@creationUser"
              + ",@cityId"
              + ",@street"
              + ",@street2"
              + ",@spatial "
              + ",@flags); "
              + "SELECT SCOPE_IDENTITY()";

    string lat = entity.SpatialLocation.Lat.Value.ToString(CultureInfo.InvariantCulture);
    string longitude = entity.SpatialLocation.Long.Value.ToString(CultureInfo.InvariantCulture);
    entity.Id = DapperExtensionsProxy.ExecuteScalar<int>(sql,new{
                                                             creationDate = entity.CreationDate,
                                                             startDate = entity.StartDate,
                                                             endDate = entity.EndDate,
                                                             creationUser = entity.CreationUser,
                                                             cityId = entity.CityId,
                                                             street = entity.Street,
                                                             street2 = entity.Street2,
                                                             flags = entity.Flags,
                                                             spatial = $"POINT({lat} {longitude} 4326)"
                                                         });
    return entity;
}