I'm used to EF 6, I'm new to EF Core. I'm trying to do a simple fetch from a table which has some foreign key relationships to lookup tables.
I started with this:
List<StatementModel> myStatements = new List<StatementModel>();
myStatements = db.Statements.Select(s => new StatementModel
{
StatmentId = s.StatementId,
EmployeeNumber = s.EmployeeNumber,
FirstName = s.Employee.FirstName,
LastName = s.Employee.LastName,
PlanType = s.Employee.PlanType != null ? s.Employee.PlanType.PlanTypeName : "",
FiscalPeriod = s.FiscalPeriod.StartDate.ToString("yyyy-MM-dd") + " - " + s.FiscalPeriod.EndDate.ToString("yyyy-MM-dd"),
CostCenterId = s.Employee.CostCenterId,
RVPName = s.Employee.CostCenter.Evp != null ? s.Employee.CostCenter.Evp.FirstName + " " + s.Employee.CostCenter.Evp.LastName : "",
SVPName = s.Employee.CostCenter.Svp != null ? s.Employee.CostCenter.Svp.FirstName + " " + s.Employee.CostCenter.Svp.LastName : "",
LOBMgrName = s.Employee.CostCenter.Lobmgr != null ? s.Employee.CostCenter.Lobmgr.FirstName + " " + s.Employee.CostCenter.Lobmgr.LastName : "",
AdminApprovalStatus = s.AdminApprovalStatus.ApprovalStatusName,
StatementStatus = s.StatementStatus.StatementStatusName,
AmountDue = s.AmountDue
}).Where(s => s.StatementStatusId == "PAA").ToList();
But it gave me a runtime error saying it couldn't translate this into SQL. I think it's a problem with .ToString()
and the date formatting.
So I changed it to:
List<StatementModel> myStatements = new List<StatementModel>();
var statements = db.Statements.Where(s => s.StatementStatusId == "PAA").ToList();
foreach (var s in statements)
{
StatementModel sm = new StatementModel();
sm.StatmentId = s.StatementId;
sm.EmployeeNumber = s.EmployeeNumber;
sm.FirstName = s.Employee.FirstName;
sm.LastName = s.Employee.LastName;
sm.PlanType = s.Employee.PlanType != null ? s.Employee.PlanType.PlanTypeName : "";
sm.FiscalPeriod = s.FiscalPeriod.StartDate.ToString("yyyy-MM-dd") + " - " + s.FiscalPeriod.EndDate.ToString("yyyy-MM-dd");
sm.CostCenterId = s.Employee.CostCenterId;
sm.RVPName = s.Employee.CostCenter.Evp != null ? s.Employee.CostCenter.Evp.FirstName + " " + s.Employee.CostCenter.Evp.LastName : "";
sm.SVPName = s.Employee.CostCenter.Svp != null ? s.Employee.CostCenter.Svp.FirstName + " " + s.Employee.CostCenter.Svp.LastName : "";
sm.LOBMgrName = s.Employee.CostCenter.Lobmgr != null ? s.Employee.CostCenter.Lobmgr.FirstName + " " + s.Employee.CostCenter.Lobmgr.LastName : "";
sm.AdminApprovalStatus = s.AdminApprovalStatus.ApprovalStatusName;
sm.StatementStatus = s.StatementStatus.StatementStatusName;
sm.AmountDue = s.AmountDue;
myStatements.Add(sm);
}
But then I got a bunch of null reference errors for any of the child objects like Employee.CostCenter
or FiscalPeriod.StartDate
.
So then I changed it to:
var statements = db.Statements.Include("Employee.CostCenter.Evp")
.Include("Employee.CostCenter.Svp")
.Include("Employee.CostCenter.Lobmgr")
.Include("FiscalPeriod")
.Include("AdminApprovalStatus")
.Include("StatementStatus").Where(s => s.StatementStatusId == "PAA").ToList();
foreach (var s in statements)
{
StatementModel sm = new StatementModel();
sm.StatmentId = s.StatementId;
sm.EmployeeNumber = s.EmployeeNumber;
sm.FirstName = s.Employee.FirstName;
sm.LastName = s.Employee.LastName;
sm.PlanType = s.Employee.PlanType != null ? s.Employee.PlanType.PlanTypeName : "";
sm.FiscalPeriod = s.FiscalPeriod.StartDate.ToString("yyyy-MM-dd") + " - " + s.FiscalPeriod.EndDate.ToString("yyyy-MM-dd");
sm.CostCenterId = s.Employee.CostCenterId;
sm.RVPName = s.Employee.CostCenter.Evp != null ? s.Employee.CostCenter.Evp.FirstName + " " + s.Employee.CostCenter.Evp.LastName : "";
sm.SVPName = s.Employee.CostCenter.Svp != null ? s.Employee.CostCenter.Svp.FirstName + " " + s.Employee.CostCenter.Svp.LastName : "";
sm.LOBMgrName = s.Employee.CostCenter.Lobmgr != null ? s.Employee.CostCenter.Lobmgr.FirstName + " " + s.Employee.CostCenter.Lobmgr.LastName : "";
sm.AdminApprovalStatus = s.AdminApprovalStatus.ApprovalStatusName;
sm.StatementStatus = s.StatementStatus.StatementStatusName;
sm.AmountDue = s.AmountDue;
myStatements.Add(sm);
}
This works, but it's very verbose and I don't recall ever having to explicitly .Include
things in EF 6 unless it was an optimization issue.
Is this the only or best way to do it in EF Core or am I missing something?