Files
Dapper/Dapper.EntityFramework/DbGeographyHandler.cs
Marc Gravell aecffecae1 attempt NRT makeover (#1928)
* attempt NRT makeover

- annotate NRTs on GridReader API
- add protected access to some of the GridReader internals
- switch GridReader callbacks to be more type-independent

docs

lib updates; deal with yellow warnings and test failures (SQL server certs and cancellation surfacing differently)

fix break

simplify proposed GridReader changes to protected OnBeforeGrid and OnAfterGrid[Async]

Builds: Bump library versions (#1935)

Tests: Upgrade dependencies for Dependabot (#1936)

I'll do a pass at all of these later, but getting CVE versions out of the pipe.

Resolving 4 alerts here: https://github.com/DapperLib/Dapper/security/dependabot

merge and lib updates

allow Identity to be constructed on-the-fly inside GridReader

fix build warnings

include readme

rev minor

- enable [SkipLocalsInit] everywhere
- use NET5_0_OR_GREATER
 for remoting check

# Conflicts:
#	Dapper/CommandDefinition.cs
#	Dapper/DefaultTypeMap.cs
#	Dapper/SqlMapper.Async.cs
#	Dapper/SqlMapper.IDataReader.cs
#	Dapper/SqlMapper.Link.cs
#	Dapper/SqlMapper.cs
#	Directory.Build.props

* shipped, not unshipped

* fixup SqlBuilder; use is null / is not null

* use central package management (#1949)

* Nullable test tweaks

* Nuget: let's just remove it!

* 2 test fixes from review

* fix FindExplicitConstructor

* add GetPropertySetterOrThrow

* fix NRT on FindConstructor

* DapperRow: value is object?

* use NotNullWhen

* make constructor problem more obvious

* test fixes

---------

Co-authored-by: Nick Craver <nrcraver@gmail.com>
2023-09-12 15:58:49 +01:00

60 lines
2.0 KiB
C#

using Microsoft.SqlServer.Types;
using System;
using System.Data;
using System.Data.Entity.Spatial;
using System.Data.SqlClient;
using System.Data.SqlTypes;
namespace Dapper.EntityFramework
{
/// <summary>
/// Type-handler for the DbGeography spatial type.
/// </summary>
public class DbGeographyHandler : SqlMapper.TypeHandler<DbGeography>
{
/// <summary>
/// Create a new handler instance.
/// </summary>
protected DbGeographyHandler() { /* create new */ }
/// <summary>
/// Default handler instance
/// </summary>
public static readonly DbGeographyHandler Default = new DbGeographyHandler();
/// <summary>
/// Assign the value of a parameter before a command executes.
/// </summary>
/// <param name="parameter">The parameter to configure.</param>
/// <param name="value">Parameter value.</param>
public override void SetValue(IDbDataParameter parameter, DbGeography? value)
{
object? parsed = null;
if (value is not null)
{
parsed = SqlGeography.STGeomFromWKB(new SqlBytes(value.AsBinary()), value.CoordinateSystemId);
}
parameter.Value = parsed ?? DBNull.Value;
if (parameter is SqlParameter sqlParameter)
{
sqlParameter.UdtTypeName = "geography";
}
}
/// <summary>
/// Parse a database value back to a typed value.
/// </summary>
/// <param name="value">The value from the database.</param>
/// <returns>The typed value.</returns>
public override DbGeography? Parse(object? value)
{
if (value is null || value is DBNull) return null;
if (value is SqlGeography geo)
{
return DbGeography.FromBinary(geo.STAsBinary().Value, geo.STSrid.Value);
}
return DbGeography.FromText(value.ToString());
}
}
}