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); }
我得到异常类型System.Data.Entity.Spatial.DbGeography不能用作参数值
我已经尝试过寻找插入的方法,this是我能找到的最好的,但它试图只插入1个参数,我该怎么做才能插入一个有dbgeography成员的对象?
更新#1
我已经放弃了试图破解或扩展内容,因为我对新手来说非常新,时间不在我身边.我回到基本这样做,因为我不需要非常频繁地进行地理数据类型插入
using (sqlConnection conn = sqlHelper.GetopenConnection()) { var sql = "INSERT INTO Stores (name,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 }); }解决方法 为核心ADO.NET程序集之外的类型添加直接支持是有问题的,因为它要么强制进行大量基于名称的反射,要么膨胀依赖项(并导致版本控制问题).在这里使用IDynamicParameters不是必需的(甚至是适当的IMO) – 相反,ICustomqueryParameter可以用来表示单个参数. DbGeography没有现有的特殊情况检测,因此除非有库更改,否则您必须执行以下 *** 作:
return conn.Execute(sql,new { store.name,store.Address,Location=store.Location.AsParameter()});
其中AsParameter()是一个扩展方法,它返回一个适当添加它的ICustomqueryParameter实现.
编辑:请点击此处查看有关此内容的更新:https://stackoverflow.com/a/24408529/23354
@H_404_0@ 总结以上是内存溢出为你收集整理的c# – 如何使用dapper将DbGeography插入SQL Server全部内容,希望文章能够帮你解决c# – 如何使用dapper将DbGeography插入SQL Server所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)