我使用 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
。我已经尝试搜索了一些插入方法,这个是我能找到的最好的方法,但它只尝试插入一个参数,那么如果要插入一个具有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
});
}