Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework changes decimal separator from passed values to FromSqlRaw

I try to execute the following query:

dbContext.Datas.FromSqlRaw($"select LD.\"Id\", LD.\"OId\" from public.\"Datas\" LD where ST_Intersects(LD.\"Geometry\", st_geomfromtext('POINT({lat} {lng})'))").Select(l => l.Id);

But whenever I'm doing it, I get the following error:

""POINT(-1741541,25" <-- parse error at position 17 within geometry" Obviously, it changes -1741541.25 to -1741541,25, and it's somehow linked with locale settings. However, my database locale is en-US, therefore it's not database's problem, but I should somehow configure my Entity framework, but I'm not sure how to do it and couldn't find it either. What should I do?

UPD: lat and lng are of type double (e.g -1741541.255287818)

I've also tried to do that using FromSqlInterpolated

dbContext.Datas.FromSqlInterpolated($"select LD.\"Id\", LD.\"OId\" from public.\"Datas\" LD where ST_Intersects(LD.\"Geometry\", st_geomfromtext('POINT({lat} {lng})'))").Select(l => l.Id); 

But I get this error:

""POINT(@p" <-- parse error at position 8 within geometry"

Also I'm pretty much sure that my query is correct. Whenever I pass those lat and lng values into the query string, it works fine.

like image 480
Aldres Avatar asked May 30 '26 09:05

Aldres


1 Answers

Went through the same situation this morning.

You can force both lat and lng to be formatted with a dot separator with .ToString(CultureInfo.InvariantCulture).

Updated after comment:

I'm not really sure, I guess the issue comes from the running Windows Regional settings / Culture as I know that in my region - and on my developing machine - the decimal separator is ,, not ..


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!