using Microsoft.SqlServer.Types;
using System;
using System.Data;
using System.Data.Entity.Spatial;
using System.Data.SqlClient;
namespace Dapper.EntityFramework
{
///
/// Type-handler for the DbGeography spatial type
///
public class DbGeographyHandler : Dapper.SqlMapper.TypeHandler
{
///
/// Create a new handler instance
///
protected DbGeographyHandler() { }
///
/// Default handler instance
///
public static readonly DbGeographyHandler Default = new DbGeographyHandler();
///
/// Assign the value of a parameter before a command executes
///
/// The parameter to configure
/// Parameter value
public override void SetValue(IDbDataParameter parameter, DbGeography value)
{
parameter.Value = value == null ? (object)DBNull.Value : (object)SqlGeography.Parse(value.AsText());
if (parameter is SqlParameter)
{
((SqlParameter)parameter).UdtTypeName = "GEOGRAPHY";
}
}
///
/// Parse a database value back to a typed value
///
/// The value from the database
/// The typed value
public override DbGeography Parse(object value)
{
return (value == null || value is DBNull) ? null : DbGeography.FromText(value.ToString());
}
}
}