'Mapping List of Lists in .NET EF from json stored in Cosmos DB container
I want to create an API endpoint using data from CosmosDB container. Data structure is as follows:
{
"id": "28.02.2022 09:31:42",
"time": 1646037099,
"states": [
[
"aa56db",
"UAL339 ",
"United States",
1646037098,
1646037099,
-84.9531,
25.9777,
11277.6,
false,
239.52,
25.85,
0,
null,
11811,
null,
false,
0,
4
],
[
"3c403f",
"FME ",
"Germany",
1646037012,
1646037012,
13.5172,
52.3712,
null,
true,
5.14,
247.5,
null,
null,
null,
null,
false,
0,
17
],
First, I have created Model "OpenSky" which looks like this:
public class OpenSky
{
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("time")]
public decimal? Time { get; set; }
[JsonProperty("states")]
public List<List<**OpenSkyFlight**>> States { get; set; }
}
Property States is the List of Lists of object type OpenSkyFlight. OpenSkyFlight Model looks like this:
public class OpenSkyFlight
{
public string Icao24 { get; set; }
public string Callsign { get; set; }
public string OriginCountry { get; set; }
public int? TimePosition { get; set; }
public int? LastContact { get; set; }
public float? Longitude { get; set; }
public float? Latitude { get; set; }
public float? BaroAltitude { get; set; }
public bool? OnGround { get; set; }
public float? Velocity { get; set; }
public float? TrueTrack { get; set; }
public float? VerticalRate { get; set; }
public string Sensors { get; set; }
public float? GeoAltitude { get; set; }
public string Squawk { get; set; }
public bool? Spi { get; set; }
public int? PositionSource { get; set; }
}
The Problem is that I am not shure how to flattern those json property "states" into object type like OpenSkyFlight. I tried the following in my Context.cs :
// OpenSky
modelBuilder.Entity<OpenSky>()
.Property(p => p.Id)
.ToJsonProperty("id");
modelBuilder.Entity<OpenSky>()
.Property(p => p.Time)
.ToJsonProperty("time");
modelBuilder.Entity<OpenSky>()
.Property(p => p.States)
.HasConversion(
v => JsonConvert.SerializeObject(v),
v => JsonConvert.DeserializeObject<List<List<object>>>(v => new Icao24 = JsonConvert.ToString(v[0]))
)
.ToJsonProperty("states");
Thank You in advance!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
