ASP.NET Embedding DbContext in Identity UserManager

I have a question model that references the AppUser model. This ratio is from 1 to * because 1 AppUser has many Questions, and the question belongs to 1 AppUser. My Question class is as follows:

public class Question
{
    public int Id { get; set; }
    public string Subject { get; set; }
    public string Text { get; set; }
    public DateTime Date { get; set; }
    public int NumOfViews { get; set; }
    public AppUser LastAnswerBy { get; set; }

    public AppUser AppUser { get; set; }
    public ICollection<Comment> Comments { get; set; }
}

So, I am trying to add a new question to the database in my controller as follows:

[HttpPost]
    public ActionResult PostQuestion(Question question)
    {
        if (ModelState.IsValid)
        {
            var id = User.Identity.GetUserId();
            var user = UserManager.Users.FirstOrDefault(x => x.Id == id);
            question.Date = DateTime.Now;
            question.AppUser = user;

            _context.Questions.Add(question);
            _context.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(question);
    }

    private AppUserManager UserManager
    {
        get { return HttpContext.GetOwinContext().GetUserManager<AppUserManager>(); }
    }

With this code, I get an exception: An entity object cannot reference multiple instances of IEntityChangeTracker. So after searching a bit, the problem seems to be that my controller and the AppUserManager class have 2 different instances of my DbContext, and the solution is to inject it into these classes. Embedding it in my controller is not a problem, but I don’t know how to insert it into my UserManager class, which looks like this:

public class AppUserManager : UserManager<AppUser>
{

    public AppUserManager(IUserStore<AppUser> store)
        : base(store)
    { }

    public static AppUserManager Create(IdentityFactoryOptions<AppUserManager> options,
    IOwinContext context)
    {
        AppIdentityDbContext db = context.Get<AppIdentityDbContext>();
        AppUserManager manager = new AppUserManager(new UserStore<AppUser>(db));

        return manager;
    }
}

IdentityConfig, :

public class IdentityConfig
{
    public void Configuration(IAppBuilder app)
    {
        app.CreatePerOwinContext<AppIdentityDbContext>(AppIdentityDbContext.Create);
        app.CreatePerOwinContext<AppUserManager>(AppUserManager.Create);
        app.CreatePerOwinContext<AppRoleManager>(AppRoleManager.Create);

        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            LoginPath = new PathString("/Account/Login"),
        });

        app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
}

, , , .

: , . :

private AppIdentityDbContext Context
    {
        get { return HttpContext.GetOwinContext().Get<AppIdentityDbContext>(); }
    }
+4
1

, , - . , , , . , :

_context = HttpContext.GetOwinContext().Get<AppIdentityDbContext>();

, , AppUserManager.

, , .:)

+3

All Articles