EDIT Driver class as requested:
public class Driver
{
public int Id { get; set; }
public ApplicationUser User { get; set; }
public string DriverDirectoryTitle { get; set; }
public string FullNames { get; set; }
public string AlternatePhoneNumber { get; set; }
public string IdNumber { get; set; }
public DateTime DateOfBirth { get; set; }
public DateTime RegistrationDate { get; set; }
public Gender Gender { get; set; }
public Ethnicity Ethnicity { get; set; }
public bool Disability { get; set; }
public string Address { get; set; }
public City City { get; set; }
public string Nationality { get; set; }
public bool OwnVehicle { get; set; }
public bool DriversLicense { get; set; }
public Experience Experience { get; set; }
public string Availability { get; set; }
public Vehicle Vehicle { get; set; }
public bool IsPremium { get; set; }
public bool Deleted { get; set; }
}
QUESTION
My app is sort of like a Euro Truck Simulator thing which lists drivers for jobs.
I'm returning a DTO onto my page (this is ASPX Web Forms by the way) which maps closely to the Driver data model:
public class DriverViewModel
{
public int Id { get; set; }
public string UserId { get; set; }
public string FullNames { get; set; }
public string DriverDirectoryTitle { get; set; }
public string RegistrationDate { get; set; }
public string AlternatePhoneNumber { get; set; }
public string IdNumber { get; set; }
public DateTime DateOfBirth { get; set; }
public Ethnicity Ethnicity { get; set; }
public Experience Experience { get; set; }
public City City { get; set; }
public Gender Gender { get; set; }
public List<DriverLicensesViewModel> LicenseCodes { get; set; }
public Vehicle Vehicle { get; set; }
public bool Disability { get; set; }
public string Address { get; set; }
public string Nationality { get; set; }
public bool OwnVehicle { get; set; }
public bool DriversLicense { get; set; }
public string Availability { get; set; }
public bool IsPremium { get; set; }
public bool ReferenceChecked { get; set; }
public bool PoliceClearance { get; set; }
}
Note that none of the relational properties are defined as virtual
- so none of the data is being lazy loaded.
There's some work that I'm doing before I return the viewmodel on the page but in particular, the mapping of only some relational properties is resulting in the error below.
Here's the code for the mapping:
using (var db = new ApplicationDbContext())
{
var drivers = db.Drivers
.Include(x => x.User).Include(x => x.City)
.Include(x => x.Ethnicity).Include(x => x.Experience)
.Include(x => x.Gender).Include(x => x.Vehicle)
.OrderBy(x => x.FullNames).ToList();
var driverModels = new List<DriverViewModel>();
foreach (var driver in drivers)
{
var driverModel = new DriverViewModel
{
Id = driver.Id,
FullNames = driver.FullNames,
DriverDirectoryTitle = driver.DriverDirectoryTitle,
RegistrationDate = driver.RegistrationDate.ToString("ddd, dd MMM yyy"),
AlternatePhoneNumber = driver.AlternatePhoneNumber,
IdNumber = driver.IdNumber,
DateOfBirth = driver.DateOfBirth,
Ethnicity = driver.Ethnicity,
Experience = driver.Experience,
Gender = driver.Gender,
Vehicle = driver.Vehicle,
Disability = driver.Disability,
Address = driver.Address,
Nationality = driver.Nationality,
OwnVehicle = driver.OwnVehicle,
DriversLicense = driver.DriversLicense,
Availability = driver.Availability,
IsPremium = driver.IsPremium,
LicenseCodes = new List<DriverLicensesViewModel>()
};
}
}
The code as presented here works fine and the data is loaded on the page perfectly, however, if I add the mapping for the City property into the mappings, the error occurs:
The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.
I've checked a few other questions such as the one linked below, but all seem to involve lazy loading the data which I'm clearly not doing:
https://stackoverflow.com/a/18398729/475766
So why does my City relation that I've added here cause the problem but none of the other relations do and how do I fix it?