简体   繁体   English

Asp.Net身份用户管理器和工作单元-如何配对?

[英]Asp.Net Identity Usermanager and Unit Of Work - how to mate them?

I am using the Unit Of Work Pattern and have defined a class for it in a separate sub-project called MyProject.Domain.DAL.UnitOfWork. 我正在使用工作单元模式,并在一个名为MyProject.Domain.DAL.UnitOfWork的单独子项目中为其定义了一个类。

Unfortunately, the whole ASP.Net Identity infrastructure is in my main project (as in the tutorial), MyProject. 不幸的是,整个ASP.Net Identity基础结构都在我的主项目(如本教程中)MyProject中。

Even more problematic: As I understand it, the ASP.NET identity uses it's own DBContext: 更成问题的是:据我所知,ASP.NET身份使用它自己的DBContext:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    /// <summary>
    /// Initializes a new instance of the <see cref="ApplicationDbContext"/> class, using the EFDB context.
    /// </summary>
    public ApplicationDbContext()
        : base("EFDBContext", throwIfV1Schema: false)
    {
    }

    /// <summary>
    /// Creates this instance.
    /// </summary>
    /// <returns>A database context</returns>
    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }
}

I have a class called Project, which should have a user associated with it - but how to do that, without using conflicting db contexts? 我有一个名为Project的类,该类应具有与其相关联的用户-但是如何在不使用冲突的数据库上下文的情况下做到这一点?

Example: 例:

public class Project
{
   public ApplicationUser User {get; set;}
}

Example calling code: 调用代码示例:

public class IndexController : Controller
{
    // uow and userManager are instantiated through Ninject
    // example code, normally this would be properties of the controller
    public ActionResult Create(IUnitOfWork uow, ApplicationUserManager userManager, int someID )
    {
       Project newProject = new Project(); 
       ApplicationUser user = await UserManager.FindByIdAsync(someID);
       // this should give a conflict, since two different DataContexts are involved
       newProject.User = user;
       uow.Projects.Insert(newProject);
       uow.Save()
       return this.View();
    }
}

} }

The template is a bit confusing. 模板有点混乱。 You shouldn't create a separate own DbContext but rather extend the existing ApplicationDbContext with the rest of the data model. 您不应创建单独的DbContext ,而应使用其余数据模型扩展现有的ApplicationDbContext You can move it to somewhere else in the project, as long as you change the type in IdentityConfig.cs . 您可以将其移动到项目中的其他位置,只要更改IdentityConfig.cs的类型即可。

There is a tougher problem to solve though when using unit of work and it is that the ASP.NET Identity methods call SaveChanges() internally and when using unit of work you typically only want that once all the work is done. 尽管使用工作单元时要解决一个更棘手的问题,那就是ASP.NET Identity方法在内部调用SaveChanges()而当使用工作单元时,通常只希望在完成所有工作后才希望。 A workaround could be to use a TransactionScope() but it could be messy to get it created before the per-request DbContext that ASP.NET Identity uses. 一种解决方法是使用TransactionScope()但在ASP.NET Identity使用的按请求的DbContext之前创建它可能很麻烦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM