ASP.NET将DbContext注入Identity UserManager

Aet*_*rna 6 .net asp.net asp.net-mvc asp.net-identity

我有一个引用AppUser模型的Question模型.这是一个1到*的关系,因为1 AppUser有很多问题,一个问题属于1 AppUser.我的问题类看起来像这样:

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; }
}
Run Code Online (Sandbox Code Playgroud)

所以我尝试在我的控制器中向数据库添加一个新问题,如下所示:

[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>(); }
    }
Run Code Online (Sandbox Code Playgroud)

使用此代码,我得到如下异常:IEntityChangeTracker的多个实例无法引用实体对象.在搜索了一下之后,似乎问题是我的控制器和AppUserManager类有两个不同的DbContext实例,解决方案是将它注入到这些类中.将它注入我的控制器是没有问题的,但我不知道如何将它注入到我的UserManager类中,如下所示:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

它从我的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);
}
Run Code Online (Sandbox Code Playgroud)

我的问题可能是我不太了解身份部分.任何帮助都非常感谢

解决方案: 正如Tobias所说我使用2种不同的上下文来更新数据库.获得这样的上下文有效:

private AppIdentityDbContext Context
    {
        get { return HttpContext.GetOwinContext().Get<AppIdentityDbContext>(); }
    }
Run Code Online (Sandbox Code Playgroud)

Tob*_*ias 5

您的问题很可能是您正在尝试在两个不同的上下文中做一件事。您正在使用一个上下文查找用户,并尝试使用另一个上下文更新数据库。要解决此问题,您应该在控制器中实例化上下文,如下所示:

_context = HttpContext.GetOwinContext().Get<AppIdentityDbContext>();
Run Code Online (Sandbox Code Playgroud)

这样,您就可以获得相同的上下文,该上下文用于实例化您的AppUserManager.

如果我写错了或者不清楚,请评论。:)