ASP.NET MVC,EF和Ninject:不同的ObjectContext问题

时间:2012-06-03 22:49:54

标签: asp.net-mvc entity-framework-4 ninject

我正在使用Ninject进行DI并将IDbContext作为构造函数参数注入我的存储库。我得到一个 “两个对象之间的关系无法定义,因为它们被附加到不同的ObjectContext对象。” 错误,同时尝试执行以下操作:

这是我的控制器的操作方法

 public ActionResult BindSpace(int spaceId, int managerId)
    {
        Space space = _spaceService.GetSpace(spaceId);
        Manager manager = _managerService.GetManager(managerId);

        if (space != null && manager != null)
        {
            _spaceService.BindManager(space, manager);
        }

        return RedirectToAction("GetSpaceBindingForm", new { id = space.Id });
    }

这是服务方法

public void BindManager(Space space, Manager manager)
    {
        if (space != null && manager != null)
        {
            space.Managers.Add(manager);
            _spaceRepo.Update(space);
        }
    }

添加和更新非相关实体时没有问题。

使用时没有问题:

ninjectKernel.Bind<IDbContext>().To<SPBSObjectContext>().InSingletonScope().WithConstructorArgument("nameOrConnectionString", "ShoppingPointBrowsingSystem");

我搜索了网络,每个人都实现并使用NinjectModule抽象基类,但我有以下代码。我在这里做错了什么?

这是注入部分:

public class NinjectControllerFactory : DefaultControllerFactory
{
    private IKernel ninjectKernel;

    public NinjectControllerFactory()
    {
        ninjectKernel = new StandardKernel();
        AddBindings();
    }

    protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType)
    {
        return controllerType == null ? null : (IController)ninjectKernel.Get(controllerType);
    }

    private void AddBindings()
    {
        // HTTP Context
        ninjectKernel.Bind<HttpContextBase>().ToMethod(context => new HttpContextWrapper(HttpContext.Current));

        // Context
        ninjectKernel.Bind<IDbContext>().To<SPBSObjectContext>().InRequestScope().WithConstructorArgument("nameOrConnectionString", "ShoppingPointBrowsingSystem");

        // Repositories
        ninjectKernel.Bind<IRepository<Admin>>().To<EfRepository<Admin>>().InRequestScope();
        ninjectKernel.Bind<IRepository<Manager>>().To<EfRepository<Manager>>().InRequestScope();
        ninjectKernel.Bind<IRepository<ShoppingCenterSpace>>().To<EfRepository<ShoppingCenterSpace>>().InRequestScope();
        ninjectKernel.Bind<IRepository<IndependentStoreSpace>>().To<EfRepository<IndependentStoreSpace>>().InRequestScope();
        ninjectKernel.Bind<IRepository<Space>>().To<EfRepository<Space>>().InRequestScope();

        // Services
        ninjectKernel.Bind<IAuthenticationService<Admin>>().To<AdminFormsAuthenticationService>();
        ninjectKernel.Bind<IAdminService>().To<AdminService>();
        ninjectKernel.Bind<IManagerService>().To<ManagerService>();
        ninjectKernel.Bind<IShoppingCenterSpaceService>().To<ShoppingCenterSpaceService>();
        ninjectKernel.Bind<IIndependentStoreSpaceService>().To<IndependentStoreSpaceService>();
        ninjectKernel.Bind<ISpaceService>().To<SpaceService>();
    }
}

1 个答案:

答案 0 :(得分:1)

InRequestScope需要Ninject.MVC3 extenison而不是自己的ControllerFactory。否则它的行为类似于InTransientScope