Tuto Recuperation de relation Entity Framework - GinierClasses/holydraw GitHub Wiki
Voici comment s'écrit un controller qui return un Player
JSON :
public async Task<ActionResult<Player>> Get()
{
return new Player(...)
}
Si vous voulez récupéré un Player
avec la Room
associé (0,1 => 0,1) :
tc._holyDrawDbContext.Player
.Include(p => p.Room)
.Find(p => p.Id)
{
"id": 1,
"username": "LapouilleLafripouille",
"avatarUrl": "Https://www.urldeouf.com",
"isOwner": false,
"disableAt": null,
"createdAt": "2021-02-03T09:32:54",
"roomId": 1,
"room": {
"id": 1,
"identifier": "AJSHAIAIO290392JIJSOAjJ\"*\"*",
"finishAt": null,
"createdAt": "2021-02-03T09:31:26",
"sessions": null,
"players": []
},
"tokens": null,
"albumSentences": null,
"createdSentences": null,
"albumDrawings": null,
"createdDrawings": null
}
Et si vous voulez y rajouter ses Token
associé ainsi que ça Room
:
var x = tc._holyDrawDbContext.Player.Include(p => t.Room).Include(p => p.Tokens).Find(p => p.Id == id)
=
{
"id": 1,
"username": "LapouilleLafripouille",
"avatarUrl": "Https://www.urldeouf.com",
"isOwner": false,
"disableAt": null,
"createdAt": "2021-02-03T09:32:54",
"roomId": 1,
"room": {
"id": 1,
"identifier": "AJSHAIAIO290392JIJSOAjJ\"*\"*",
"finishAt": null,
"createdAt": "2021-02-03T09:31:26",
"sessions": null,
"players": []
},
"tokens": [
{
"id": null,
"tokenKey": "3IKRBUpv",
"discardAt": null,
"createdAt": "2021-02-05T19:23:42.851503+01:00",
"playerId": 1
},
{
"id": 25,
"tokenKey": "fO2-LuDw",
"discardAt": null,
"createdAt": "2021-02-05T19:23:43",
"playerId": 1
},
{
"id": 1,
"tokenKey": "TOK::AJ323NANJ::AJJAJ",
"discardAt": null,
"createdAt": "2021-02-03T09:34:52",
"playerId": 1
},
],
"albumSentences": null,
"createdSentences": null,
"albumDrawings": null,
"createdDrawings": null
}
Et si par exemple vous voulez en + récupéré Players
de la Room
:
tc._holyDrawDbContext.Player
.Include(p => p.Room)
.Include(p => p.Tokens)
.Include(p => p.Room.Players)
.Find(p => p.Id)