Ninject:实体对象不能被IEntityChangeTracker的多个实例引用

Ninject: entity object cannot be referenced by multiple instances of IEntityChangeTracker

我开始在我的 MVC5 代码优先应用程序中使用 Ninject。这是我的 NinjectWebCommon.cs:

private static IKernel CreateKernel()
    {
        var kernel = new StandardKernel();
        try
        {
            kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
            kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();

            kernel.Bind<CMSContext>()
                .ToSelf()
                //.InSingletonScope();
                .InRequestScope();

            kernel.Bind<IExecutiveRepository>()
                .To<ExecutiveRepository>();

            kernel.Bind<IExecutiveSectionRepository>()
                .To<ExecutiveSectionRepository>();

            kernel.Bind<IExecutiveSectionMappingRepository>()
                .To<ExecutiveSectionMappingRepository>();

            kernel.Bind<IUserRepository>()
                .To<UserRepository>();

            kernel.Bind<IContentRepository>()
                .To<ContentRepository>();

            RegisterServices(kernel);
            return kernel;
        }
        catch
        {
            kernel.Dispose();
            throw;
        }
    }

我尝试了 .InSingletonScope() 和 .InRequestScope() 但我仍然收到“实体对象无法被 IEntityChangeTracker 的多个实例引用”错误。 这是我的界面:

    public interface IExecutiveRepository : IDisposable
{
    IEnumerable<Executive> GetExecutives();
    Executive GetExecutiveById(int executiveId);
    void InsertExecutive(Executive executive);
    void UpdateExecutive(Executive executive);
    void DeleteExecutive(int executiveId);
    void Save();
}

这是我的具体做法:

 public class ExecutiveRepository : IExecutiveRepository, IDisposable
{
    private CMSContext context;

    public ExecutiveRepository(CMSContext context)
    {
        this.context = context;
    }

    public IEnumerable<Executive> GetExecutives()
    {
        return context.Executives.ToList();
    }

    public Executive GetExecutiveById(int id)
    {
        return context.Executives.Find(id);
    }

    public void InsertExecutive(Executive executive)
    {
        context.Executives.Add(executive);
    }

    public void DeleteExecutive(int executiveId)
    {
        Executive executive = context.Executives.Find(executiveId);
        context.Executives.Remove(executive);
    }

    public void UpdateExecutive(Executive executive)
    {
        context.Entry(executive).State = EntityState.Modified;
    }

    public void Save()
    {
        context.SaveChanges();
    }

    private bool disposed = false;

    protected virtual void Dispose(bool disposing)
    {
        if (!this.disposed)
        {
            if (disposing)
            {
                context.Dispose();
            }
        }
        this.disposed = true;
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }
}

这是控制器(顶部相关部分):

 public class ExecutiveController : Controller
{
    private IExecutiveRepository executiveRepository;
    private IUserRepository userRepository;
    private IExecutiveSectionRepository executiveSectionRepository;
    private IExecutiveSectionMappingRepository executiveSectionMappingRepository;
    private IContentRepository contentRepository;
    private Ninject.IKernel _kernel = new StandardKernel();

    //[Inject]
    public ExecutiveController()
    {
        executiveRepository = _kernel.Get<ExecutiveRepository>();
        userRepository = _kernel.Get<UserRepository>();
        executiveSectionRepository = _kernel.Get<ExecutiveSectionRepository>();
        executiveSectionMappingRepository = _kernel.Get<ExecutiveSectionMappingRepository>();
        contentRepository = _kernel.Get<ContentRepository>();
    }
 ...

不确定我做错了什么,但在添加新的 'Executive' 时它会爆炸...我确实理解它正在尝试使用单独的上下文,这就是问题所在,但我只是不确定如何修理它。显然,NinjectWebCommon.cs class 中的行:

 kernel.Bind<CMSContext>()
                .ToSelf()
                //.InSingletonScope();
                .InRequestScope();

应该是解决方法,但不是... 任何 ideas/suggestions?

这可能无法回答问题。但我倾向于使用 EF 为您提供的 IDbContextFactory 并执行如下操作:

public interface IDefaultContextFactory : IDbContextFactory<CMSContext> {}

public class DefaultContextFactory : IDefaultContextFactory 
{
    private readonly Lazy<CMSContext> lazyContext = new Lazy<CMSContext>(() => new CMSContext());

    public CMSContext Create() 
    {
        return lazyContext.Value;
    }
}

然后你只需绑定它,当你需要上下文时,你可以这样做:

public class ExecutiveRepository : IExecutiveRepository, IDisposable
{
    private readonly CMSContext context;

    public ExecutiveRepository(IDefaultContextFactory contextFactory)
    {
        this.context = contextFactory.Create();
    }
}

我相信@BatteryBackupUnit 是正确的,我也会考虑将上述模式用于上下文。

您正在为每个控制器创建一个内核。

InRequestScope 仅确保每个请求一个实例每个内核

因此您需要调整内核设置,以便每个 Web 应用程序只有一个内核。参见:

你应该使用 NUGET 包 Ninject.Web.Mvc 如果你还没有。这会将您的应用程序配置为可以使用 Ninject,而不是您的绑定。从我在您的 CreateKernel() 方法中看到的内容来看,您似乎已经相当熟悉事物的绑定方面。

绑定到位后,您不应在控制器中创建内核,这是因为 Ninject.Web.Mvc 库配置 Ninject 以在后台为您创建控制器。因此,您添加到它们的任何依赖项都应该自动解析。

因此,您可以使用构造函数注入来解决您的依赖关系:

public class ExecutiveController : Controller
{
    private IExecutiveRepository ExecutiveRepository;
    private IUserRepository UserRepository;
    private IExecutiveSectionRepository ExecutiveSectionRepository;
    private IExecutiveSectionMappingRepository ExecutiveSectionMappingRepository;
    private IContentRepository ContentRepository;

    public ExecutiveController(
         IExecutiveRepository executiveRepository,
         IUserRepository userRepository,
         IExecutiveSectionRepository executiveSectionRepository,
         IExecutiveSectionMappingRepository executiveSectionMappingRepository,
         IContentRepository contentRepository)
    {

         // Set the field values
         this.ExecutiveRepository = executiveRepository,
         this.UserRepository = userRepository,
         this.ExecutiveSectionRepository = executiveSectionRepository,
         this.ExecutiveSectionMappingRepository = executiveSectionMappingRepository,
         this.ContentRepository = contentRepository;
    }

    public ActionResult Index(int id)
    {
        // Use one of your dependencies...
        var executive = this.executiveRepository.GetExecutiveById(id);
    }
}

或者你可以使用具有相同效果的[Inject]属性:

public class ExecutiveController : Controller
{
    [Inject]
    public IExecutiveRepository executiveRepository { get; set; }

    [Inject]
    public IUserRepository userRepository { get; set; }

    [Inject]
    public IExecutiveSectionRepository executiveSectionRepository { get; set; }

    [Inject]
    public IExecutiveSectionMappingRepository executiveSectionMappingRepository { get; set; }

    [Inject]
    public IContentRepository contentRepository { get; set; }

    public ExecutiveController()
    {

    }

    public ActionResult Index(int id)
    {
        // Use one of your dependencies...
        var executive = this.executiveRepository.GetExecutiveById(id);
    }
}