System.ObjectDisposedException with Virtual property in entity framework

asp.net-mvc c# entity-framework

Question

Hello, can someone assist with this? When I try to display information about the Carmodels in my view, I get the error mentioned above.

        [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; }

Here is the function, too.

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();
        }
1
5
2/9/2014 8:43:55 AM

Accepted Answer

ZZZ_tmp
5
2/7/2014 6:14:24 PM

Popular Answer

It is attempting to load the lazyModel following the return of the list, property (and theDbContext is eliminated). you can eagerly load theModel disable proxy creation or the sluggish loading property.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow