Hi can anybody help with this I am getting the above error when trying to display data about the Carmodels in my view
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int CarId { get; set; }
[Required]
public string Registration { get; set; }
[Required]
public virtual CarModels Model { get; set; }
[Required]
public string RegistrationYear { get; set; }
[Required]
public string ChassisNumber { get; set; }
[Required]
public int RegistrationId { get; set; }
And here is the function
public static List<Cars> GetRegistrationCars(int registration)
{
List<Cars> registrationCars = new List<Cars>();
using (var db = new EventsContext())
{
registrationCars = db.Cars.Where(c => c.RegistrationId == registration).ToList();
}
return registrationCars.ToList();
}
Ah ha figured it out in the end Thanks for the suggestions
public static List<Cars> GetRegistrationCars(int registration)
{
List<Cars> registrationCars = new List<Cars>();
using (var db = new FerrariEventsContext())
{
registrationCars = db.Cars.Include(m=> m.Model).Where(c => c.RegistrationId == registration).ToList();
}
return registrationCars;
}
It's attempting to lazy load the Model
property after the list is returned (and the DbContext
is disposed). Either eager load the Model
property or disable lazy loading/proxy generation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With