Al momento ho un oggetto Report
. Questo oggetto report ha più sottoentità, come WorkOrder
e ReleaseQuestions
. Volevo solo chiedere la migliore pratica per aggiungere WorkOrder
e ReleaseQuestions
al database. Attualmente, quando viene creato un report, aggiungo nuovi oggetti vuoti all'oggetto report, quindi li aggiungo al database. Questo perché alla fine questi componenti saranno compilati dall'utente, quindi avere le righe vuote nel database non fa molto male, ma non sono sicuro che questa sia la migliore pratica. Sarebbe meglio se aggiungessi i nuovi componenti separatamente? O è quello che sto facendo non troppo lontano?
Codice, per le persone che sono più visive:
public async Task<ReportModel> AddReport(ReportModel reportModel)
{
// CapacityPlanReport report = _mapper.Map<ReportModel, CapacityPlanReport>(reportModel);
var report = new CapacityPlanReport
{
AddedYear = DateTime.Now.ToString(),
Type = reportModel.Type,
Eta = reportModel.Eta
};
// Create the corresponding pieces of the report in the database.
report.ReleaseQuestion = new CapacityPlanReleaseQuestion();
report.WorkOrder = new CapacityPlanWorkOrder();
_context.CapacityPlanReport.Add(report);
await _context.SaveChangesAsync();
var result = await GetReport(report.CapacityPlanReportId);
return result;
}
Se i componenti verranno compilati dall'utente in un secondo momento , basta non riempirli affatto quando si inserisce solo il Report
e si usano null
s. Quando l'utente ti fornisce i dati appropriati, crea l'entità, assegnala al report e SaveChanges
- EF aggiornerà il tuo Report
esistente e inserirà la subentità nel db insieme a tutti i riferimenti FK necessari.
È probabilmente più naturale per qualcosa che non esiste ancora essere null
piuttosto che un'istanza vuota. Un'istanza vuota è qualcosa dopo tutto.