简体   繁体   中英

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.

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

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.

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. You IoC container is responsible for instantiating and disposing of the created objects as you instruct it.

So you will typically have this:

IoC registration:

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. If your not using Ninject then your IoC will most likely have something similar.

Under Ninjects binding for the context i had to use the .InRequestScope() method.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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