简体   繁体   English

ASp.net身份如何为EF使用相同的dbcontext?

[英]ASp.net Identity How to use the same dbcontext for EF?

I am trying to use the same DbContext for ASP.NET Identity and my other work - otherwise bad things happen. 我正在尝试对ASP.NET Identity和其他工作使用相同的DbContext-否则会发生不好的事情。 Unfortunately, I am not able to achieve this - I end up using two DbContexts, which are causing doubled objects etc. 不幸的是,我无法实现这一目标-我最终使用了两个DbContext,它们导致对象翻倍。

I am not sure what code is needed to debug this...ask if I missed something. 我不确定调试此代码需要什么代码...如果我错过了什么,请询问。

Startup: 启动:

public partial class Startup
{
    public void ConfigureAuth(IAppBuilder app)
    {
        app.CreatePerOwinContext(PriorityDBContext.Create);
        app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);

        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            LoginPath = new PathString("/Account/Login"),
            Provider = new CookieAuthenticationProvider
            {
                OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
                    validateInterval: TimeSpan.FromMinutes(30),
                    regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
            }
        });

        app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
    }
}

PriorityDBContext PriorityDBContext

public class PriorityDBContext : ApplicationDbContext
    {
        public PriorityDBContext()
            : base()
        {
        }

// seems to be called twice :(
        public static new ApplicationDbContext Create()
        {
            return new PriorityDBContext() as ApplicationDbContext;
        }

 // snipped my DbSets
    }
}

The ApplicationDbContext ApplicationDbContext

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("EFDBContext", throwIfV1Schema: false)
    {
    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }
}

And in my main controller: 在我的主控制器中:

protected override IAsyncResult BeginExecuteCore(AsyncCallback callback, object state)
{

    var httpContext = System.Web.HttpContext.Current;
    ApplicationDbContext pbContext httpContext.GetOwinContext().Get<ApplicationDbContext>();

    this.UserManager = httpContext.Request.GetOwinContext().GetUserManager<ApplicationUserManager>();
    this.uoW = new UnitOfWork(pbContext);

    return base.BeginExecuteCore(callback, state);
}

Ok, never mind - the error was quite obvious and basically a result of leftovers from the original state. 好的,没关系-该错误非常明显,并且基本上是原始状态中剩余的结果。 My Unit of Work initialization looked like this: 我的工作单元初始化如下所示:

/// </summary>
private PriorityDBContext dbContext = new PriorityDBContext();

public UnitOfWork()
{ 
}

public UnitOfWork(ApplicationDbContext dbContext)
{
    this.dbContext = (PriorityDBContext)ApplicationDbContext;
}

a new PriorityDbContext was created for each new Unit of Work. 为每个新的工作单元创建一个新的PriorityDbContext。 The dbContext passed into the Constructor was never used. 从未使用传递到构造函数的dbContext。

Now it looks like this: 现在看起来像这样:

    private PriorityDBContext dbContext = null;

    public UnitOfWork()
    {
        PriorityDBContext dbContext = new PriorityDBContext();
    }

    public UnitOfWork(ApplicationDbContext dbContext)
    {
        this.dbContext = dbContext as PriorityDBContext;
    }

Problem solved ;) 问题解决了 ;)

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

相关问题 EF代码优先迁移的ASP.NET身份DbContext问题 - ASP.NET Identity DbContext Issue with EF Code First Migrations Asp.net身份DbContext - Asp.net Identity DbContext 当ASP.net Identity与相同的DbContext正常工作时,为什么EF7表示我没有配置任何提供程序? - Why does EF7 say I haven't configured any providers, when ASP.net Identity is working fine with the same DbContext? 如何配置Ninject来注入我的DbContext与ASP.NET Identity Framework一起使用? - How can I configure Ninject to inject my DbContext for use with ASP.NET Identity Framework? 如何在asp.net core和ef7中的并行方法中使用注入的DbContext? - How to use injected DbContext in parallel methods in asp.net core and ef7? 如何将 EF Code First DbContext 绑定到 Asp.Net 数据源? - How to bind EF Code First DbContext to an Asp.Net DataSource? 如何使用 EF Core 更新 ASP.NET 5 中的 dbcontext 脚手架? - How to update dbcontext scaffolding in ASP.NET 5 with EF Core? 将ASP.NET标识集成到现有的DbContext中 - Integrating ASP.NET Identity into Existing DbContext 关于ASP.NET身份环境中的DbContext - About DbContext in an ASP.NET Identity environment 如果我使用ASP.NET身份,我是否坚持使用EF? - If I use ASP.NET identity, am I stuck with EF?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM