How to save webapi data into database in asp.net core 3.1

Alice 61 Reputation points
2021-06-29T20:37:01.343+00:00

I have to fetch data from a Web API and save it into a database.

I'm able to fetch data from the Web API and deserialize it, now I'm stuck on how to save that data into a SQL Server database using EF Core.

I know how to do it in a ASP.NET Core app, but here it's a deserialized string, so I'm a bit confused.

I'm stuck cause when I tried to create the migration for db entity using the json structure, Visual Studio tells me that 3 classes doesn't have primary Id key, so I added them. After adding those Id key, then it creates the migration.

When using below EF Core saving code, I got a type errors, I know that the database and Web API data are different type that's why... but I'm not sure how to save Web API data to the database.

await context.Crypto.AddAsync(cryptoDb);
await context.SaveChangesAsync();
So far, I got 2 entities class, 1 for the json map when deserializing and the other one for the db when saving.

Below is the code to fetch Web API data it's works well, but I'm unable to save it to the db.

I use https://coinmarketcap.com/api/ to fetch that data.

I got the data thru (https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest?CMC_PRO_API_KEY=" + API_KEY;) you just need to add the key. it's free of charge. you can also use the one I provided below.

This code works fine:

public class MyClass
{
private static readonly string API_KEY = "edec1506-1010-4373-8983-89c9ef470efe";
readonly string Base_Url = "https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest?CMC_PRO_API_KEY=" + API_KEY;

public async Task<Crypto> GetData()
{
        string url = Base_Url;

        HttpClient client = new HttpClient();
        HttpResponseMessage responseMessage = await client.GetAsync(url);

        if (responseMessage.StatusCode == System.Net.HttpStatusCode.OK)
        {
            var result = await responseMessage.Content.ReadAsStringAsync();

            var deserializedClass = JsonConvert.DeserializeObject<Crypto>(result);

            return deserializedClass;
        }

        return null;
}

}
The DTO/json class is consist of 6 small classes.

public class Status
{
public DateTime Timestamp { get; set; }
public int Error_code { get; set; }
public object Error_message { get; set; }
public int Elapsed { get; set; }
public int Credit_count { get; set; }
public object Notice { get; set; }
public int Total_count { get; set; }
}

public class Platform
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Symbol { get; set; }
    public string Slug { get; set; }
    public string Token_address { get; set; }
}

public class USD
{
    public double Price { get; set; }
    public double Volume_24h { get; set; }
    public double Percent_change_1h { get; set; }
    public double Percent_change_24h { get; set; }
    public double Percent_change_7d { get; set; }
    public double Percent_change_30d { get; set; }
    public double Percent_change_60d { get; set; }
    public double Percent_change_90d { get; set; }
    public double Market_cap { get; set; }
    public DateTime Last_updated { get; set; }
}

public class Quote
{
    public USD USD { get; set; }
}

public class Datum
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Symbol { get; set; }
    public string Slug { get; set; }
    public int Num_market_pairs { get; set; }
    public DateTime Date_added { get; set; }
    public List<string> Tags { get; set; }
    public long? Max_supply { get; set; }
    public double Circulating_supply { get; set; }
    public double Total_supply { get; set; }
    public Platform Platform { get; set; }
    public int Cmc_rank { get; set; }
    public DateTime Last_updated { get; set; }
    public Quote Quote { get; set; }
}

public class Root
{
    public Status Status { get; set; }
    public List<Datum> Data { get; set; }
}

Below is the json that I converted into c# object in https://json2csharp.com/

You can copy paste it to the above site to get the C# classes It generates 6 small classes

Please I'm really stuck, can you help ?

{
"data": [
{
"id": 1,
"name": "Bitcoin",
"symbol": "BTC",
"slug": "bitcoin",
"cmc_rank": 5,
"num_market_pairs": 500,
"circulating_supply": 16950100,
"total_supply": 16950100,
"max_supply": 21000000,
"last_updated": "2018-06-02T22:51:28.209Z",
"date_added": "2013-04-28T00:00:00.000Z",
"tags": [
"mineable"
],
"platform": null,
"quote": {
"USD": {
"price": 9283.92,
"volume_24h": 7155680000,
"percent_change_1h": -0.152774,
"percent_change_24h": 0.518894,
"percent_change_7d": 0.986573,
"market_cap": 158055024432,
"last_updated": "2018-08-09T22:53:32.000Z"
},
"BTC": {
"price": 1,
"volume_24h": 772012,
"percent_change_1h": 0,
"percent_change_24h": 0,
"percent_change_7d": 0,
"market_cap": 17024600,
"last_updated": "2018-08-09T22:53:32.000Z"
}
}
},
{
"id": 1027,
"name": "Ethereum",
"symbol": "ETH",
"slug": "ethereum",
"num_market_pairs": 6360,
"circulating_supply": 16950100,
"total_supply": 16950100,
"max_supply": 21000000,
"last_updated": "2018-06-02T22:51:28.209Z",
"date_added": "2013-04-28T00:00:00.000Z",
"tags": [
"mineable"
],
"platform": null,
"quote": {
"USD": {
"price": 1283.92,
"volume_24h": 7155680000,
"percent_change_1h": -0.152774,
"percent_change_24h": 0.518894,
"percent_change_7d": 0.986573,
"market_cap": 158055024432,
"last_updated": "2018-08-09T22:53:32.000Z"
},
"ETH": {
"price": 1,
"volume_24h": 772012,
"percent_change_1h": 0,
"percent_change_24h": 0,
"percent_change_7d": 0,
"market_cap": 17024600,
"last_updated": "2018-08-09T22:53:32.000Z"
}
}
}
],
"status": {
"timestamp": "2018-06-02T22:51:28.209Z",
"error_code": 0,
"error_message": "",
"elapsed": 10,
"credit_count": 1
}
}

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,133 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Duane Arnold 3,211 Reputation points
    2021-06-29T21:10:34.18+00:00
    0 comments No comments