我已经创建了模型using System.Data.Entity.Spatial;
public class Store
{
public int Id { get; private set; }
public string Name { get; set; }
public string Address { get; set; }
public DbGeography Location { get; set; }
}
插入数据库
using (SqlConnection conn = SqlHelper.GetOpenConnection())
{
const string sql = "INSERT INTO Stores(Name, Address, Location) " +
"VALUES (@Name, @Address, @Location)";
return conn.Execute(sql, store);
}
我得到了例外type System.Data.Entity.Spatial.DbGeography cannot be used as a parameter value
我试过寻找插入的方法,这是我能找到的最好的方法,但它试图只插入 1 个参数,我应该怎么做才能插入一个具有 dbgeography 成员的对象?
更新#1
我已经放弃了尝试破解或扩展东西,因为我对 dapper 很陌生,而且时间目前不在我身边。我回到了基本做这件事,因为我不需要非常频繁地进行地理数据类型插入
using (SqlConnection conn = SqlHelper.GetOpenConnection())
{
var sql = "INSERT INTO Stores (Name, Address, IsActive, Location, TenantId) " +
"VALUES('@Name', '@Address', @IsActive, geography::Point(@Lat,@Lng, 4326), @TenantId);";
return conn.Execute(sql, new
{
Name = store.Name,
Address = store.Address,
IsActive = store.IsActive,
Lat = store.Location.Latitude.Value,
Lng = store.Location.Longitude.Value,
TenantId = store.TenantId
});
}