EF预加载映射City属性触发ObjectContext已释放错误排查
Hey there, let's break down why this error only pops up when you add the City property to your DriverViewModel, and walk through how to fix it.
What's Causing the Issue?
Even though you're using .Include(x => x.City) to preload the City entity, here's the key problem:
- Your
DriverViewModelholds a direct reference to the EFCityentity. - When the
usingblock ends, yourApplicationDbContextgets disposed. Later, when ASP.NET Web Forms tries to bind theDriverViewModelto UI controls (like a GridView or Repeater), it might attempt to access unloaded navigation properties on the City entity. Since the context is already gone, EF can't lazy-load those properties, hence the error.
Other related entities (like Ethnicity or Experience) likely don't have additional navigation properties, or they're enum types—so binding them doesn't trigger lazy-load attempts. That's why they work without issues.
Fix 1: Use a CityViewModel (Recommended)
The safest, most maintainable approach is to avoid passing EF entities directly to your view layer. Create a lightweight CityViewModel that only includes the properties you need to display:
public class CityViewModel { public int Id { get; set; } public string Name { get; set; } // Add only the properties your UI needs—no navigation properties }
Update your DriverViewModel to use this view model instead of the EF City entity:
public class DriverViewModel { // ... existing properties ... public CityViewModel City { get; set; } // ... rest of your properties ... }
Finally, map the City entity to CityViewModel in your loop:
foreach (var driver in drivers) { var driverModel = new DriverViewModel { // ... existing property mappings ... City = new CityViewModel { Id = driver.City.Id, Name = driver.City.Name }, // ... remaining mappings ... }; driverModels.Add(driverModel); // Don't forget to add to your list! }
This eliminates any EF entity references that could trigger lazy-loading after the context is disposed.
Fix 2: Preload All City Navigation Properties (Fragile Short-Term Fix)
If you must use the EF City entity directly, ensure you preload every navigation property your UI might access. For example, if City has a State property, update your query:
var drivers = db.Drivers .Include(x => x.User) .Include(x => x.City).ThenInclude(x => x.State) // Add ThenInclude for City's navigation properties .Include(x => x.Ethnicity) .Include(x => x.Experience) .Include(x => x.Gender) .Include(x => x.Vehicle) .OrderBy(x => x.FullNames).ToList();
⚠️ Heads up: This approach is fragile—if you add a new navigation property to City later, you'll have to remember to add another ThenInclude, otherwise the error will return. Stick with view models for long-term stability.
Quick UI Binding Check
Double-check your Web Forms markup to ensure you're not binding to any City properties that aren't preloaded. For example, if you have <%# Eval("City.State.Name") %> in your markup but didn't include City.State in your Include chain, that's exactly what would trigger the error.
内容的提问来源于stack exchange,提问作者Ortund

