简体   繁体   中英

Entity Framework not getting Spatial type data in result from Sql Server stored procedure

I'm using Entity Framework 6 with .Net 4.5 . I have a stored procedure that selects and returns data. One of the return columns is a geography type . In Visual Studio 2015, I right click the .edmx file, click "Update Model From Database...". This action gets my stored procedure and creates a complex type of storeprocedurename_Result. All the columns are represented in the complex type objects except the geography type .

At the same time one of my table also have Geography type but that table is retrieved successfuly with the geography column and it is functioning properly. So, i can guess geography type is supported in EF and is working.
But i don't know why it is not generating that type for me in the complex type generated.
I tried manually adding public System.Data.Entity.Spatial.DbGeography col_name { get; set; } but it got null in results.
For the record i have tried executing the procedure in Sql Server management studion and that column is present there. So, its problem on EF end not DB end.
I have searched and got a couple of questions like these, but it seems no one is able to answer them.
I was not able to understand the response on this question.
http://www.scriptscoop.net/t/2758a3668fde/geography-column-returned-in-stored-procedure-not-shown-in-entity-fram.html

and it is one on stackoverflow
https://stackoverflow.com/questions/28592085/geography-column-returned-in-stored-procedure-not-shown-in-entity-framework-auto# =
The person asking above question has used a workaround as he says in comment "No. I don't think you can automatically get the geography type. Because when you view the results in the model browser it says "Not supported". To get around this issue, my stored proc converts the geography type to a string via [pro_GeoLocation].STAsText() as pro_GeoLocationPoint. Then I use a regex in my C# code to get the long and lat." But this is not a good option and there must be a solution to this.

Alright...
As i was expecting (seeing previous posts like this) there was no answer to this problem, so now when i have solved the problem, i will linke to share the solution which got me the geography object.

I tried many things and tried many solution, i dropped my procedure and SP entry from edmx many times. And re created but all in vain!
Than what worked was so simple... following the link below i just opened edmx, than open model browser, in model browser go to your complex type defination which is missing the column, right click add->scalar property->geography. And give it the name precisely which is retured by stored procedure.
Thats it, save the changes and run your code, you must get your data properly as i am getting.
I am wondering why EF was not able to get the the data and on getting column information of the function/SP it mentioned EDM type unsupported. Strage!

Ref: http://www.scriptscoop.net/t/7c1ed5a0f89e/entity-framework-5-function-import-with-spatial-data.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM