In my .Net Core 3.1 Web app I have a Class shared by Backend and Frontend that looks like this
public class Order
{
[Key]
[Required]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public DateTime OrderTime { get; set; }
public int UserId { get; set; }
public int Vat { get; set; }
[NotMapped]
public Dictionary<string, int> Products { get; set; }
public int Discount { get; set; }
public float ShippingPrice { get; set; }
public bool Shipped { get; set; }
public bool Cancelled { get; set; }
public string CancelReason { get; set; }
public Order()
{
}
}
And on Frontend I am using HttpClient to Get a list of Orders from REST API.
Json that the httpClient receives looks like:
[
{
"id": 1,
"orderTime": "2021-01-28T14:55:03.077",
"userId": 0,
"vat": 0,
"products": null,
"discount": 0,
"shippingPrice": 0,
"shipped": true,
"cancelled": true,
"cancelReason": "string"
},
{
"id": 2,
"orderTime": "2021-01-28T14:55:03.077",
"userId": 2,
"vat": 0,
"products": null,
"discount": 10,
"shippingPrice": 0,
"shipped": false,
"cancelled": false,
"cancelReason": null
}
]
And for deserialization I am using JsonSerializer:
var returnOrders = await JsonSerializer.DeserializeAsync<List<Order>>(await response.Content.ReadAsStreamAsync());
From that I do get the correct amount of objects on List, but all of them have values of 0 or null etc. What am I doing wrong?
Before I was using ReadAsAsync() which worked fine, but is deprecated in .Net core 3
await response.Content.ReadAsAsync<List<Object>>();
Order.Productshas value? I mean does your queryIncludes the mapped entities there? Is your data correct, I mean should be data there?Order.Productsthan null when there is no data there in the database then I would initiateOrder.Productsfield int he constructor. As a result it will be a list having zero element. Is this what you are looking for?