DbContext在使用语句中调用ToList方法后被释放。
DbContext在使用语句中调用ToList方法后被释放。
尽管代码使用了返回List
public ListGetEntityByProperty(string property) { using (AppDbContext appDbContext = GetContext()) { List entities = appDbContext.Entities .Where(e => e.Property == property) .ToList(); return entities; } }
这又被一个web服务类调用:
private static EntityRepository EntityRepositoryStatic = new EntityRepository(); [WebMethod] public ListGetEntityByProperty(string property) { return EntityRepositoryStatic.GetEntityByProperty(property); }
EntityRepository继承自一个实现了IDisposable接口(用于释放上下文)的Repository基类,并具有返回上下文实例的代码:
private AppDbContext appDbContext; public AppDbContext GetContext() { if (appDbContext == null) { appDbContext = new AppDbContext(); } return appDbContext; }
异常是在存储库中对appDbContext进行调用的代码行上抛出的。我是否遗漏了一些明显的东西?
由于这是一个庞大的遗留代码库,因此我们只是尝试用存储库类的调用替换web服务中的内联上下文调用。
DbContext在using语句块内调用ToList()方法后被释放,导致DbContext已被释放,尽管在using语句块内调用ToList()方法。解决方法是要么在每次调用GetContext()之后不释放DbContext,要么在每次使用完DbContext后进行释放,不能同时使用这两种方式。
问题的原因是,第一次调用GetContext()方法时,创建了一个新的DbContext对象,并将其返回。然后,该DbContext对象被使用和释放。接下来的对GetContext()方法的调用将返回已经被释放的原始DbContext对象,从而导致错误的发生。如果想要重复使用DbContext对象,则不能对其进行释放;如果想要在使用完后进行释放,则不能重复使用它。
下面是解决该问题的代码示例:
public class MyClass { private DbContext _context; public DbContext GetContext() { if (_context == null) { _context = new DbContext(); } return _context; } public void DoSomething() { using (var context = GetContext()) { var entities = context.Entities.ToList(); // do something with entities } } }
在上面的示例中,首次调用GetContext()方法时会创建一个新的DbContext对象,并将其存储在私有变量_context中。在DoSomething()方法中,使用using语句块来确保在使用完DbContext后进行释放。这样就可以避免DbContext已被释放的错误发生。
请注意,上述示例中的代码仅用于说明问题的解决方法,并不是完整的实现。具体的实现方式可能因项目的具体情况而有所不同。