To comment further, you need to separate your concerns. You should not use a database context similar to this in the controller. Rather, use it through the repository or service level.
I also had a problem using using . I deleted the used part. Modify the code below to fit your scenario. Assuming you should return a list of users. I would do this in my repository class:
public class UserRepository : IUserRepository { MyDbContext dbContext = new MyDbContext(); public IEnumerable<User> GetAll() { return dbContext.Users; } }
This repository, which you then injected into your Autofac, Ninject controller, etc.
In your controller, it will look something like this:
public class UserController : Controller { private readonly IUserRepository userRepository; public UserController(IUserRepository userRepository) { this.userRepository = userRepository; } public ActionResult Index() { UserViewModel viewModel = new UserViewModel { Users = userRepository.GetAll() }; } }
And then, in your opinion, you can simply skip users.
Brendan vogt
source share