简体   繁体   中英

Return type error when i'm getting JSON as result when calling stored procedure using entity framework

I was trying to get output by calling stored procedure to get result type in JSON format by calling the API.I have used Entity Framework and mapping was done to get values.But i'm getting the error.

i'm using visual studio 2019(entity framework 6,WEB API 2 using entity),SQL Server 2017

[HttpGet]
[Route("api/display_specifications/test/{tid}")]           
public string GetUsers(int tid)
{
  using (projectEntities users = new projectEntities())
  {
    return users.get_dynamicform(tid).FirstOrDefault();
  }
}

EXPECTED OUTPUT:

{"field_id":1,"button_id":1,"label_name":"Full_Name","datatype":1,"ranges":60,"default_value":"null","field_width":20,"field_height":100,"label_width":10,
"rownumber":1,"sequence":1,"visibility":1,"placeholder":"Enter Full Name"}

ERROR:

The type parameter 'System.String' in ExecuteFunction is incompatible with the type 'testingModel.get_dynamicform_Result' returned by the function for json

In EF the stored procedure behind get_dynamicform is defined to return type get_dynamicform_Result which means it returns a collection. The use of FirstOrDefault means you will have zero or one instances of get_dynamicform_Result .

But GetUsers says it returns a string, and no conversion exists from get_dynamicform_Result to string .

You probably want to return a member property of get_dynamicform_Result rather than the whole object (but without the definition of get_dynamicform_Result we cannot be more specific). And also you need to handle the null case.

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