How to retrieve data from multiple tables in JSON.
I have a system which records the cars a customer has bought
Tables are:
Customer - Id, Name
CarPurchases - Id, CustomerID, ManufacturerID, ColorChosen
Manufacturer - Id, ManName
The Manufacturer table is a look up table with car manufacturers (Audi, BMW, Mercedes, Ferrari etc)
One customer can purchase multiple cars (maybe one for themselves, wife etc) but we note down the color.
So far this is ok. I use EF 6 to retrieve the data:
myContext.CarPurchases.GetAll();
On a webpage all looks ok and as required however now i need to have this data available in Json so i created a webAPI project. When i return the data with the above code the JSON is heavily nested and doesnt look very friendly to use.
How can i rerieve the data so it return the
CustomerName, ManufacturerName, ColorChosen
Example of some data
Customer
Id - Name
1 Tony
2 Bob
CarPurchases
Id - CustomerID - ManufacturerID - ColorChosen
1 1 2 Blue
2 1 2 Red
3 1 4 Black
4 2 2 Custom Yellow
Manufacturer
Id - ManName
1 Audi
2 BMW
3 Mercedes
4 Ferrari
So again how do i retrieve this data in a nice format without having heavy nesting when i get CarPurchases? Do i create a new Model and reference this in or craete a separate method with Joins etc? If you can provide any examples that would be helpful.