mvc dbcontext无法在Azure上运行

时间:2013-04-12 15:46:31

标签: c# asp.net-mvc entity-framework dbcontext

我正在创建一个处理器类并在worker角色中运行。 在处理器内部,我创建了reporitory来对我的数据库进行CRUD操作。

我有2个存储库,产品和产品,dbcontext是在产品仓库中创建的,并传递给产品尺寸。

我的本​​地计算机上的一切运行正常,但是当我部署到Azure时,系统会抛出这个错误:

System.InvalidOperationException: The operation cannot be completed because the DbContext has been disposed.
   at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
   at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
   at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
   at System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext()
   at System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider()
   at System.Linq.Queryable.Where[TSource](IQueryable`1 source, Expression`1 predicate)

processor.cs

  public class Processor : IDisposable
    {

        private readonly IProductRepository _productRepository;

        public Processor()
            : this(new ProductRepository())
        {

        }

        public Processor(
            IProductRepository productRepository

            )
        {
            _productRepository = productRepository;
        }

        public void Run(){ _productRepository.someoperation;}

        public void Dispose()
        {

            _productRepository.Dispose();

        }

productrepository.cs

        public class ProductRepository : IProductRepository
        {


        private static readonly DbContext_context = new DbContext();

        private readonly IProductSizeRepository _productSizeRepository;


        public ProductRepository()
            : this(
            new ProductSizeRepository(_context))
        {

        }

        public ProductRepository(
            IProductSizeRepository productSizeRepository
           )
        {

            _pictureRepository = pictureRepository;

        }

   public void Dispose()
        {
            _context.Dispose();


            _productSizeRepository.Dispose();


        }

在workerrole.cs while循环中     使用(var runner = new Processor())                 {                      runner.Run();                 }

1 个答案:

答案 0 :(得分:0)

需要将所有引用副本设置为local = true

并在代码中而不是在构造函数

中启动dbcontext