Vorrei chiedere come includere i dati nel mio caso. Ho creato una tabella associativa in cui definisco la relazione tra BusinessGlosItems
. Nel passaggio successivo ho creato il modello di BusinessGlosItems
e ho incluso la tabella di riferimento tramite le relazioni Child e Parent.
Infine ho bisogno di visualizzare i dettagli di BusinessGlosItems
con tutti i dati associati. Sembra facile, ma non so come risolverlo. Qualcuno può aiutare per favore?
Le mie classi di dominio sono le seguenti:
public class BusinessGlosItem
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string BGIName { get; set; }
[InverseProperty("ChildBGItem")]
public ICollection<Reference> ChildReferences { get; set; }
[InverseProperty("ParentBGItem")]
public ICollection<Reference> ParentReferences { get; set; }
}
public class Reference
{
public int Id { get; set; }
[ForeignKey("ChildBGItem")]
public int? ChildGlosItemId { get; set; }
[ForeignKey("ParentBGItem")]
public int? ParentGlosItemId { get; set; }
public int ReferenceTypeId { get; set; }
public virtual ReferenceType ReferenceType { get; set; }
public virtual BusinessGlosItem ChildBGItem { get; set; }
public virtual BusinessGlosItem ParentBGItem { get; set; }
}
Cosa mi piacerebbe fare:
@page
@model BusinessGlosItem.DetailsModel
<div>
<table class="table table-hover">
<th>Reference Type</th>
<th>Parent Item Name</th>
<th>Child Item Name</th>
@foreach (var item in Model.BusinessGlosItem.ParentReferences)
{
<tr>
<td>@item.ReferenceType.ReferenceTypeName</td>
<td>@item.ParentBGItem.BGIName</td>
<td>@item.ChildGlosItemId</td> //Here I would like to see the ChildItem Name
</tr>
}
</table>
</div>
Ricevo NullReferenceException: Object reference not set to an instance of an object. on the line
dalla vista.
Il mio metodo è:
public BusinessGlosItem BusinessGlosItem { get; set; }
public async Task<IActionResult> OnGetAsync(int? id)
{
if (id == null)
{
return NotFound();
}
BusinessGlosItem = await _context.businessGlosItem
.Include(x => x.BusinessArea)
.Include(x => x.BusinessGlosItemStatus)
.Include(x => x.ChildReferences).ThenInclude(x=>x.ReferenceType)
.Include(x => x.ParentReferences).ThenInclude(x=>x.ReferenceType)
.Include(x=>x.businessGlosItemComments)
.SingleOrDefaultAsync(m => m.Id == id);
if (BusinessGlosItem == null)
{
return NotFound();
}
return Page();
}
Grazie mille in anticipo!
Hai dimenticato di menzionare ThenInclude
per ChildBGItem
e ParentBGItem
in ChildReferences
e ParentReferences
. Quindi la tua query dovrebbe essere la seguente:
BusinessGlosItem = await _context.businessGlosItem
.Include(x => x.BusinessArea)
.Include(x => x.BusinessGlosItemStatus)
.Include(x => x.ChildReferences)
.ThenInclude(x=>x.ReferenceType)
.Include(x => x.ChildReferences) // You missed this include
.ThenInclude(x=>x.ChildBGItem)
.Include(x => x.ChildReferences) // You missed this include
.ThenInclude(x=>x.ParentBGItem)
.Include(x => x.ParentReferences)
.ThenInclude(x=>x.ReferenceType)
.Include(x => x.ParentReferences) // You missed this include
.ThenInclude(x=>x.ChildBGItem)
.Include(x => x.ParentReferences) // You missed this include
.ThenInclude(x=>x.ParentBGItem)
.Include(x=>x.businessGlosItemComments)
.SingleOrDefaultAsync(m => m.Id == id);
Ora dovrebbe funzionare.