Search code examples
nhibernatefluent-nhibernatesql-server-2012geography

Nhibernate 'Not all named parameters have been set' when using spatial query


This is my query

var selectDinnerByDistance = string.Format(
            @"Select 
                u.ProfileImageUrl as ProfileImageUrl, 
                d.Starter as Starter,
                d.Main as Main,
                d.Dessert as Dessert,
                d.Dry as DryDinner,
                d.[Date] as EventDate,
                l.GeoLoc.STDistance(geography::STGeomFromText('POINT({0} {1})', 4326)) as Distance
                from dbo.Locations l
                join Dinners d on d.LocationId = l.Id
                join Users u on u.Id = d.UserId
                Order by Distance asc
                OFFSET {2} ROWS
                FETCH NEXT {3} ROWS ONLY"
            , lat, lng, skip, take);
var output = _session
     .CreateSQLQuery(selectDinnerByDistance)
     .SetResultTransformer(Transformers.AliasToBean<DinnerListItemDto>())
     .List<DinnerListItemDto>();

And the exception I'm getting is

Not all named parameters have been set: [':STGeomFromText']

The query works fine in management studio. Is there another way that I should be using straight sql queries to avoid this error?

Thanks


Solution

  • As I stated in the comments above. The solution was to rewrite the query in a different way I replaced "geography::STGeomFromText" with

    l.GeoLoc.STDistance(@dist.STBuffer(0.2).STAsText()) as Distance
    

    and placed the following at the top of the query

    DECLARE @dist AS Geography = 'POINT({0} {1})'