C# 为什么消息无法访问已释放的上下文实例。。。?

C# 为什么消息无法访问已释放的上下文实例。。。?,c#,dependency-injection,entity-framework-core,C#,Dependency Injection,Entity Framework Core,我在一段时间后收到一条消息:无法访问已释放的上下文实例。此错误的一个常见原因是处理通过依赖项注入解析的上下文实例,然后在应用程序的其他位置尝试使用相同的上下文实例。如果对上下文实例调用“Dispose”,或将其包装到using语句中,则可能会发生这种情况。如果您使用的是依赖项注入,那么应该让依赖项注入容器处理上下文实例 虽然我认为我遵循规则使用services.AddScoped作为我的一般存储。我不知道该做些什么来防止被处置。我将尝试重新启动进程。。。但是我该怎么办呢?我错了什么?我不知道 这

我在一段时间后收到一条消息:无法访问已释放的上下文实例。此错误的一个常见原因是处理通过依赖项注入解析的上下文实例,然后在应用程序的其他位置尝试使用相同的上下文实例。如果对上下文实例调用“Dispose”,或将其包装到using语句中,则可能会发生这种情况。如果您使用的是依赖项注入,那么应该让依赖项注入容器处理上下文实例

虽然我认为我遵循规则使用services.AddScoped作为我的一般存储。我不知道该做些什么来防止被处置。我将尝试重新启动进程。。。但是我该怎么办呢?我错了什么?我不知道

这是我的密码。第一部分是概述,第二部分是登记。它工作了很长时间,然后砰的一声。。。错误消息

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;

namespace MyData.Repository
{
    public class GenericRepository<T> : IGenericRepository<T> where T : class
    {
        private myDbContext _context = null;
        private DbSet<T> table = null;
        public GenericRepository()
        {
            this._context = new myDbContext();
            table = _context.Set<T>();
        }
        public GenericRepository(myDbContext context)
        {
            this._context = context;
            table = context.Set<T>();
        }

        public IQueryable<T> Where(Expression<Func<T, bool>> filter)
        {
            return table.Where(filter);
        }

        public IEnumerable<T> GetAll()
        {
            return table.ToList();
        }
        public async Task<T> GetById(object id)
        {
            return await table.FindAsync(id);
        }
        public async Task Insert(T obj)
        {
            await table.AddAsync(obj);
        }

        public async Task Update(int id, T obj)
        {
            var entry = await table.FindAsync(id);
            _context.Entry(entry).CurrentValues.SetValues(obj);
        }

        public async Task Delete(int id)
        {
            T existing = await table.FindAsync(id);
            table.Remove(existing);
        }

        public async Task Save()
        {
           await _context.SaveChangesAsync();
        }
    }
}

using MyData.Repository;
using Microsoft.Extensions.DependencyInjection;

namespace MyData.Configuration
{
    public static class RegisterDependencyServiceDataLayer
    {

        public static void ConfigureService(IServiceCollection services)
        {
            services.AddScoped(typeof(IGenericRepository<>), typeof(GenericRepository<>));
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Linq.Expressions;
使用系统文本;
使用System.Threading.Tasks;
使用Microsoft.EntityFrameworkCore;
命名空间MyData.Repository
{
公共类GenericRepository:IGenericRepository,其中T:class
{
私有myDbContext _context=null;
私有DbSet表=null;
公共通用存储库()
{
这个._context=new myDbContext();
表=_context.Set();
}
公共GenericRepository(myDbContext上下文)
{
这._context=context;
table=context.Set();
}
公共IQueryable Where(表达式筛选器)
{
返回表。其中(过滤器);
}
公共IEnumerable GetAll()
{
return table.ToList();
}
公共异步任务GetById(对象id)
{
返回等待表FindAsync(id);
}
公共异步任务插入(T obj)
{
wait table.AddAsync(obj);
}
公共异步任务更新(int-id,T-obj)
{
var entry=wait table.FindAsync(id);
_context.Entry(Entry).CurrentValues.SetValues(obj);
}
公共异步任务删除(int-id)
{
T existing=wait table.FindAsync(id);
表2.移除(现有);
}
公共异步任务保存()
{
wait_context.SaveChangesAsync();
}
}
}
使用MyData.Repository;
使用Microsoft.Extensions.DependencyInjection;
名称空间MyData.Configuration
{
公共静态类RegisterDependencyServiceDataLayer
{
公共静态无效配置服务(IServiceCollection服务)
{
服务.addScope(typeof(IGenericRepository)、typeof(genericpository));
}
}
}

当上下文无法保存时,为什么要处理/更新上下文?这就像把你的车开到破坏者的院子里被压碎,仅仅因为它没油了?这就是我尝试的,因为我不知道如何解决它。。。我删除了那部分。。。我希望有人能帮我。95%的几率你错过了
等待
某个地方。
\u上下文
在哪里?为什么不从DI注入它呢?我总是想知道为什么人们会为这样的类而烦恼。您所有的方法几乎都是围绕上下文中等价命名的方法的单行包装器;你得到了什么?直接使用上下文方法?