我已经看到很多关于此错误的问题,很遗憾再次提问,但到目前为止还没有解决方案对我有用。
我正在使用Ninject和Ninject.Web.Common引用的ASP.NET Web API项目,其中DbContext被注入到存储库中。第二次我随后发送请求时会弹出此错误。
我的堆栈跟踪:
at System.Data.Entity.Internal.InternalContext.CheckContextNotDisposed()
at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
at System.Data.Entity.Internal.InternalContext.Initialize()
at System.Data.Entity.Internal.LazyInternalContext.get_ObjectContext()
at System.Data.Entity.Internal.InternalContext.DetectChanges(Boolean force)
at System.Data.Entity.Internal.InternalContext.GetStateEntries(Func`2 predicate)
at System.Data.Entity.Internal.InternalContext.GetStateEntries()
at System.Data.Entity.Infrastructure.DbChangeTracker.Entries()
at Project.Data.UnitOfWork.Commit() in c:\Projects\Project\src\DotNet\Project.Data\UnitOfWork.cs:line 22
at Project.Api.Infrastructure.UnitOfWorkAttribute.OnActionExecuted(HttpActionExecutedContext actionExecutedContext) in c:\Projects\Project\src\DotNet\Project.Api\Infrastructure\UnitOfWorkAttribute.cs:line 25
at System.Web.Http.Filters.ActionFilterAttribute.<CallOnActionExecutedAsync>d__1.MoveNext()
Ninject:RegisterServices
kernel.Bind<DataContextFactory>().ToSelf().InRequestScope().WithConstructorArgument("nameOrConnectionString", "Project");
kernel.Bind<DataContext>().ToMethod(context => kernel.Get<DataContextFactory>().GetContext()).InRequestScope();
kernel.Bind<IRepository<Device>>().To<Repository<Device>>();
kernel.Bind<IRepository<Person>>().To<Repository<Person>>();
kernel.Bind<UnitOfWork>().ToSelf().InRequestScope();
kernel.BindHttpFilter<UnitOfWorkAttribute>(FilterScope.Controller).WhenControllerType<DeviceController>();
kernel.BindHttpFilter<UnitOfWorkAttribute>(FilterScope.Controller).WhenControllerType<PersonController>();
我的IRepositiry类
public interface IRepository<TAggregateRoot> where TAggregateRoot : class, IAggregateRoot
{
void Save(TAggregateRoot instance);
void Remove(TAggregateRoot instance);
TAggregateRoot One(Expression<Func<TAggregateRoot, bool>> predicate = null, params Expression<Func<TAggregateRoot, object>>[] includes);
IQueryable<TAggregateRoot> All(Expression<Func<TAggregateRoot, bool>> predicate = null, params Expression<Func<TAggregateRoot, object>>[] includes);
bool Exists(Expression<Func<TAggregateRoot, bool>> predicate = null);
int Count(Expression<Func<TAggregateRoot, bool>> predicate = null);
}
我的人控制器类
public class PersonController : ApiController
{
private readonly IRepository<Person> _personRepository;
public PersonController(IRepository<Person> personRepository)
{
_personRepository = personRepository;
}
...
}
我的DbContext
private readonly IDictionary _configurations;
public DataContext(string nameOrConnectionString, IDictionary<MethodInfo, object> configurations)
: base(nameOrConnectionString)
{
_configurations = configurations;
}
public virtual void MarkAsModified<TEntity>(TEntity instance) where TEntity : class
{
Entry(instance).State = EntityState.Modified;
}
public virtual IDbSet<TEntity> CreateSet<TEntity>() where TEntity : class
{
return Set<TEntity>();
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
if (modelBuilder == null)
{
throw new ArgumentNullException("modelBuilder");
}
foreach (var config in _configurations)
{
config.Key.Invoke(modelBuilder.Configurations, new[] { config.Value });
}
base.OnModelCreating(modelBuilder);
}