How to embed DbGeography in SQL Server using dapper

I created a model 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; }
}

Insert into DB

using (SqlConnection conn = SqlHelper.GetOpenConnection())
{
    const string sql = "INSERT INTO Stores(Name, Address, Location) " + 
                       "VALUES (@Name, @Address, @Location)";
    return conn.Execute(sql, store);                                
}

I get an exception type System.Data.Entity.Spatial.DbGeography cannot be used as a parameter value

I tried looking for ways to insert, this is the best I can find, but it is trying to insert only one parameter, what should I do in order to insert an object that has a dbgeography member?

Update # 1

I have given up trying to hack or expand stuff since I am very new to dapper and time is now on my side. I returned to the main implementation of this, since I do not need to enter the type of geographic data very often.

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
            });             
        }
+4
source share
1 answer

ADO.NET , , ( ). IDynamicParameters ( , IMO), ICustomQueryParameter . DbGeography, , , - :

return conn.Execute(sql, new {
    store.Name, store.Address, Location=store.Location.AsParameter()
});

AsParameter() - , ICustomQueryParameter, .


: . : fooobar.com/questions/1545944/...

+1

Source: https://habr.com/ru/post/1545943/


All Articles