1

I had done a project of using stored procedure to display join table values using entity framework. I had done all the basic procedure and while calling it from my model it successfully return the object result,

Now my problem is I cannot able to convert that result model from stored procedure which is of complex return type to my view model. Can some one help me in solving it.

I had attached my project workings below,

This is the auto generated context by ADO .net Entity model

enter image description here

Edit function in model browser function imports

enter image description here

Display result which is auto generated.

enter image description here

This is what i'm trying to do

enter image description here

And this is the error I'm getting

enter image description here

This is how my view model consist of

enter image description here

Stored procedure query (Mysql)

enter image description here

Jawahar05
  • 354
  • 8
  • 17
  • you have to provide mapping for your `ViewModel` in the `.Select()` like:`var query = objEmployee.sp_display().Select(s=>new ViewModel{employees=new employee{//add the mapping properties here},employee_detail = new employee_detail{//add the mapping properties here}}).ToList()`. – vikscool Jul 16 '19 at 04:58
  • @vikscool will you show me a sample example for that add mapping properties location, I have name and department in employee table and address and mobile in employee details – Jawahar05 Jul 16 '19 at 05:17

1 Answers1

2

As mentioned in the comment by OP the schema of employee and employee_details is something like:

public class employee
{
    public string Name {get;set;}
    public string Department {get;set;}
}

public class employee_details
{
    public string Address {get;set;}
    public string Mobile {get;set;}
}

So, after applying the mapping your code would look something like:

var query = objEmployee.sp_display().Select(s=> new ViewModel{
employees = new employee{
 Name = s.Name //change the s.Name to the property name coming from your SP(if different)
 Department = s.Department
},
employee_detail = new employee_detail{
  Address = s.Address,
  Mobile = s.Mobile
}}).ToList()

Or Better just remove the query object and use the listEmployeeList as this list itself is also referencing the ViewModel so the query object is un-necessary(unless you want to do some other filtration on it) :

listEmployeeList = // the above code with .ToList();
vikscool
  • 1,293
  • 1
  • 10
  • 24
  • As I displayed above in the 3rd image there are no properties written were the stored procedure return the value. Since it is auto-generated any future changes will overwrite if we define the properties manually, even though I defined it manually it assigning the number of index list of "listEmployeeList" but it dosen't store any value, It just displays a null value. Am I missing any mapping during the model creation? – Jawahar05 Jul 16 '19 at 06:43
  • It would display null as you have no properties defined in the `sp_display_result` class. If the columns can change from the `Procedure` then the `ViewModel` class as well have to change. So, it is better to provide mapping properties in the `sp_display_result` class. Else use a `DataTable` to capture the data and then convert it to `json` and pass it to client/handle in business logi to support mapping. – vikscool Jul 16 '19 at 07:59
  • Since there is no input parameter in the procedure, How to map with sp_display_result.?Is there any way to map. – Jawahar05 Jul 16 '19 at 09:11