頭痛がするので、これを行うことを学びたいです:S
ゴール:
アプリの論理 JSON オブジェクトを取得するための DB クエリをできるだけ少なくする。
シナリオ
親が子供の活動費を支払うことができる学校向けのアプリ。親には多くの生徒がいて、生徒には多くのラインアイテム (注文) があり、ラインアイテムは生徒と製品の間の結合テーブルとして機能します。
モデル:
public class Parent
{
public int ParentId { get; set; }
public string FullName { get; set; }
public virtual ICollection<Student> Students { get; set; }
}
public class Student
{
public int StudentId { get; set; }
public string FullName { get; set; }
public int ParentId { get; set; }
public virtual Parent Parent { get; set; }
public virtual ICollection<LineItem> LineItems { get; set; }
}
public class Product
{
public int ProductId { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
public virtual ICollection<LineItem> LineItems { get; set; }
}
public class LineItem
{
public int Id { get; set; }
public int ProductId { get; set; }
public int StudentId { get; set; }
public int Discount {get; set;}
// ...
public virtual Product Product { get; set; }
public virtual Student Student { get; set; }
}
生成したい JSON:
{
"Parent": {
"ParentId": 10,
"FullName": "John Doe",
"Students": [
{
"StudentId": 12,
"FullName": "William Doe",
"ParentId": 10,
"LineItems": [
{
"Discount": 10,
"Price": 150,
"Name": "Student trip to Washington"
},
{
"Discount": 10,
"Price": 20,
"Name": "Halloween party"
}
]
},
{
"StudentId": 15,
"FullName": "Kate Mary-Jane Doe",
"ParentId": 10,
"LineItems": [
{
"Discount": 10,
"Price": 110,
"Name": "Spring Break to Mexico"
}
]
}
]
}
}
ノート:
View Models と AutoMapper を試しましたが、すべてのネストされた出力を取得できません。私ができる最善のことは、ネストされた Parent -> Students[] 出力を取得することでしたが、LineItems と Products にアクセスする方法がわかりませんでした。