Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing Ninject with Entity Framework 5 when I have a BaseController
    text
    copied!<p>My current stack is ASP.NET MVC 4 and Entity Framework 5.0. I installed ninject.mvc3 by NuGet and the code showed below works fine:</p> <pre><code>public class SessionsController : Controller { // use "kernel.Bind&lt;MyContext&gt;().ToSelf().InRequestScope();" // to inject MyContext private MyContext _context; public SessionsController(MyContext context) { _context = context; } [HttpGet] public ActionResult Login() { System.Diagnostics.Debug.WriteLine(_context.Users.Count()); return View(); } } </code></pre> <p>}</p> <p>Now, I want to extract a BaseController for my controllers: </p> <pre><code>public class BaseController : Controller { protected MyContext _context; public BaseController(MyContext context) { _context = context; } // I don't know what should be write here and // base controller must have a parameterless constructor public BaseController() { } } </code></pre> <p>Then I make SessionsController inherit from BaseController. When I run the code, an exception throwed that </p> <blockquote> <p>"Object reference not set to an instance of an object.(with MyContext)"</p> </blockquote> <p>Do I use Ninject wrong?</p> <p><strong>--UPDATED--</strong> the ninject's NinjectWebCommon.cs's code</p> <pre><code> /// &lt;summary&gt; /// Load your modules or register your services here! /// &lt;/summary&gt; /// &lt;param name="kernel"&gt;The kernel.&lt;/param&gt; private static void RegisterServices(IKernel kernel) { kernel.Bind&lt;TelesingContext&gt;().ToSelf().InRequestScope(); } </code></pre> <p><strong>--UPDATED--</strong> the edited SessionsController.cs code</p> <pre><code>public class SessionsController : BaseController { [HttpGet] public ActionResult Login() { System.Diagnostics.Debug.WriteLine(_context.Users.Count()); return View(); } } </code></pre> <p>}</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload