简体   繁体   English

实体框架dbContext处理为时过早

[英]Entity Framework dbContext is disposed too early

I am trying to make my code work with Dependency Injection but I am having a few issues. 我试图使我的代码与Dependency Injection一起工作,但出现了一些问题。

I have the following code which fetches a User and the associated Roles. 我有以下代码可提取用户和关联的角色。

public virtual User GetUser(string username,string password,evolutiondbEntities context, IUserRole userRoleRepository)
        {
            User systemUser = new User();
            using(context)
            {
                systemUser = (from u in context.Users where u.Username == username && u.UserPassword == password select u).FirstOrDefault();
                List<IUserRole> roleList = userRoleRepository.GetRoles(systemUser.UserID);
                systemUser._roles = roleList;
            }

            return systemUser;
        }

The code for the GetRoles Method is as follows GetRoles方法的代码如下

public List<IUserRole> GetRoles(string userID,evolutiondbEntities context)
        {
            List<IUserRole> roleList = new List<IUserRole>();
            using(context)
            {
                roleList = (from r in context.UserRoles where r.UserID == userID select r).ToList<IUserRole>();
            }

            return roleList;
        }

The code fetches the user correctly but then when it calls the GetRoles() method the context appears to have been disposed and so therefore fails. 该代码正确地提取了用户,但是当它调用GetRoles()方法时,上下文似乎已被释放,因此失败。

NOTE: I know I should be passing in an interface for the context but I have not got that far yet. 注意:我知道我应该为上下文传递一个接口,但是我还没走那么远。

You should inject the context into your service and use it without using block as at the end of using block the context is disposed. 您应该将上下文注入服务中,并在不using block的情况下使用它,因为在using上下文结束后using block时。 You IoC container is responsible for instantiating and disposing of the created objects as you instruct it. 您的IoC容器负责按照您的说明实例化和处置创建的对象。

So you will typically have this: 因此,您通常会有以下内容:

IoC registration: IoC注册:

container.For<Context>().Use<Context>();

And in you service: 为您服务:

public class SomeService : ISomeService
{
    private readonly Context _context;
    private readonly IUserRole _userRoleRepository;
    public SomeService(Context context, IUserRole userRoleRepository)
    {
        _context = context;
        _userRoleRepository = userRoleRepository;
    }

    public virtual User GetUser(string username, string password)
    {
        User systemUser = new User();         
        systemUser = (from u in _context.Users where u.Username == username && u.UserPassword == password select u).FirstOrDefault();
        List<IUserRole> roleList = _userRoleRepository.GetRoles(systemUser.UserID);
        systemUser._roles = roleList;          

        return systemUser;
    }
}

I've had a similar issue in the past, using Ninject. 过去,我使用Ninject遇到过类似的问题。 If your not using Ninject then your IoC will most likely have something similar. 如果您不使用Ninject,则您的IoC很可能具有类似的东西。

Under Ninjects binding for the context i had to use the .InRequestScope() method. 在上下文的Ninjects绑定下,我必须使用.InRequestScope()方法。

kernel.Bind<EmployeeDbContext>().ToSelf().InRequestScope();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM