使用实体框架的缓存问题与Ninject MVC3工具 [英] MVC3 tool using Entity Framework caching issues with Ninject

查看:122
本文介绍了使用实体框架的缓存问题与Ninject MVC3工具的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这显示出在数据库中手动修改数据时,一些问题的新的MVC 3应用程序。

I've got a new MVC 3 application which is showing some issues when modifying data manually in the Database.

工具仍在开发中,一旦在一段时间我想改变我的用户teamId。当我这样做,我要杀死Web开发服务器,然后再次运行它,否则查询不挑新teamId。
同样的事情时,我发布工具IIS,如果我修改数据库上的东西,我需要或者再次复制过的bin文件夹或停止应用程序并重新运行它。

The tool is still in development and once in a while I want to change my user's teamId. When I do so, I have to kill the Web development Server and run it again otherwise the queries don't pick the new teamId. Same thing when I publish the tool to IIS, if I ever modify something on the database, I need to either copy over the 'bin' folder again or stop the application and re-run it.

当我从应用程序本身的修改数据,我没有问题。

When I modify data from the application itself, I have no problems.

这是我的Ninject的样子:

This is how my Ninject looks like:

public class NinjectControllerFactory : DefaultControllerFactory
{
    private IKernel kernel = new StandardKernel(new TrackerServices());

    protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
    {
        if (controllerType == null)
            return null;
        return (IController)kernel.Get(controllerType);
    }

    private class TrackerServices : NinjectModule
    {
        public override void Load()
        {
            var context = new TrackerEntities();

            Bind<IUserRepository>().To<UserRepository>().WithConstructorArgument("context", context);
        }
    }
}

我的接口:

public interface IUserRepository : IRepository<User>
{
    User GetByName(string name);
}

我的执行情况:

public User GetByName(string login)
{
    var userLogin = _misc.GetUsername(login);
    return _context.Users.Where(x => x.Login == userLogin).Single();
}

和我的索引操作

public ActionResult Index()
{
    var teamid = (int)_users.GetByName("myName").TeamId;

这是从来没有过,但是这个工具是我使用的是Ninject第一个。我想知道如果有我的问题之间的关系,并使用资料库?

This has never happened before, but this tool is the first one I'm using with Ninject. I'm wondering if there's a relation between my problem and using a repository?

推荐答案

有被相结合,创造该问题的两个问题:

There are two issues that are combining to create this problem:


  1. 您已经创建了上下文的方式是使其有效地成为一个单例。

  2. 实体框架将不会自动检查其上下文已经跟踪实体的新版本。

要解决这个问题,我建议你重新创建存储库每一次请求(不会有此一显著的性能损失,因为它是相当轻巧),通过使用此绑定:

To solve this, I would recommend that you recreate your repository once per request (there won't be a significant performance hit for this, as it's fairly lightweight), by using this binding:

绑定&LT; IUserRepository&GT;()为&lt;。UserRepository方式&gt;()InRequestScope();

Ninject应该能够自动创建您的 TrackerEntities 的背景下,但如果没有(或者,如果你想清楚),可以使用以下绑定:

Ninject should be able to create your TrackerEntities context automatically, but if not (or if you want to make it clear), you can use the following binding:

绑定&LT; TrackerEntities&GT;()ToSelf()InRequestScope(); (即 InRequestScope 是不是真的这里需要作为默认的瞬间范围应该没问题)。

Bind<TrackerEntities>().ToSelf().InRequestScope(); (The InRequestScope is not really required here, as the default transient scope should be okay).

您也可以下去迫使实体的刷新之路(使用 ObjectContext.Refresh()),但是这可能不是一个好主意,因为你会必须为每个实体做明确。

You could also go down the road of forcing a refresh of the entity (using ObjectContext.Refresh()), but that's probably not a great idea because you'd have to do it explicitly for each entity.

这篇关于使用实体框架的缓存问题与Ninject MVC3工具的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆