Quantcast
Channel: Web API
Viewing all articles
Browse latest Browse all 4850

Web API - Getting SQL Server Database Data into defined models

$
0
0

Hello everyone,

my question is the following:

Let's say, i want to build a Web API, the easy way.

That means i only want to have Controllers, Models, Global.asax and some kind of Database Context.

My Model looks like this:

namespace web_api
{
    public class Country
    {
        public int id {get; set;}
        public string name {get; set;}
        public City[] City {get; set;}

    }

    public class City
    {
        public int id {get; set;}
        public string name {get; set;}
        public Citizen[] Citizen {get; set;}
    }

    public class Citizen
    {
       public int id {get; set;}
       public string name {get; set;}
       public int age {get; set;}
    }


My Database looks like this:

Entity Country

id        | name
______________________
1           Germany
2           USA
......

Entity City

id        | name         | country_id
__________________________________________
1           Munich         1
2           Los Angeles    2

Entity Citizen

id        | name         | age         | city_id
_________________________________________________
1           Peter Pan      35             1
2           Winney ThePooh 50             2

And finally i want to get a JSON that looks like this:

[
    {"id":1,"name":Germany,"city": [
        {"id":1,"name":"munich""citizen": [
              {"id":1,"name":"Peter Pan",
"age":35 } ] } ] } ]

The database (Sql Server) is already set up and the data is also exisiting (incl. Foreign Keys, Primary Keys...)

I'm looking for a possible way to get the data from the database into my Web API. 

Many thanks in advance.


Viewing all articles
Browse latest Browse all 4850

Trending Articles