How to get ApplicationDbContext out of the Owin pipeline

前端 未结 1 1550
别那么骄傲
别那么骄傲 2021-02-19 16:30

This has to be simple, but I\'m going bug-eyed trying to find the answer. How does a controller action get a reference to the per-request ApplicationDbContext that was stashed

相关标签:
1条回答
  • 2021-02-19 16:35

    And the answer (apparently) is... You need to add this using statement to get it to work:

    using Microsoft.AspNet.Identity.Owin;
    

    so a complete example would look like:

    using Microsoft.AspNet.Identity.Owin;
    
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            var context = HttpContext.GetOwinContext().Get<ApplicationDbContext>();
            DoSomething(context); // Use the context object; do not dispose it!
    
            return View();
        }
    }
    
    0 讨论(0)
提交回复
热议问题