EntityFramework-Reverse-POCO-Code-First-Generator icon indicating copy to clipboard operation
EntityFramework-Reverse-POCO-Code-First-Generator copied to clipboard

Nullable geography type in stored procedure

Open tuol opened this issue 4 years ago • 2 comments

I have a MSSQL stored procedure that has the parameter.

@GeoLocation geography = null

Poco is generating the following:

var geoLocationParam = new SqlParameter 
{
    ParameterName = "@GeoLocation", 
    UdtTypeName = "geography", 
    Direction = ParameterDirection.Input,
    Value = Microsoft.SqlServer.Types.SqlGeography.Parse(geoLocation.AsText()),
    Size = -1
};

if (geoLocationParam.Value == null) 
    geoLocationParam.Value = DBNull.Value;

When I call my POCO generated stored procedure and provide a null value for my geoLocation parameter (System.Data.Entity.Spatial.DbGeography), .AsText() will throw a NullReferenceException.

Alternatively, when I provide an DbGeography.PointFromText("POINT EMPTY",4326) to represent an IsEmpty DbGeography, the parsed value geoLocationParam.Value == "POINT EMPTY" instead of null. This means that DBNull.Value is not passed to the procedure.

Any ideas how to solve this?

I am using NuGet Microsoft.SqlServer.Types 14.0.1016.290.

tuol avatar May 06 '21 17:05 tuol

Could you show me your stored procedure here. Just the basics of what I need to test this myself. And how you are calling it. Thanks.

sjh37 avatar May 07 '21 08:05 sjh37

Thanks for the quick comment!

Step 1: Add stored procedure

CREATE PROCEDURE My_PocoTest @GeoLocation geography 
AS
    IF @GeoLocation IS NOT NULL THROW 50001, '@GeoLocation is expected to be NULL', 1
GO

Step 2: Generate the model I used v3.3.0 - EF6.

Try 1: Call the stored procedure with null value

dbContext.MyPocoTest(null);

Try 2: Call the stored procedure with empty DbGeography point

var emptyPoint = System.Data.Entity.Spatial.DbGeography.PointFromText("POINT EMPTY", 4326);
dbContext.MyPocoTest(emptyPoint);

In both scenarios I think I would expect a DBNull.Value to be passed, but both fail.

Personally, I think passing null seems the most intuitive way to use the API.

tuol avatar May 07 '21 16:05 tuol