Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Entity framework ASP.net标识、IoC和共享DbContext_Entity Framework_Autofac_Asp.net Identity_Owin_Asp.net Web Api2 - Fatal编程技术网

Entity framework ASP.net标识、IoC和共享DbContext

Entity framework ASP.net标识、IoC和共享DbContext,entity-framework,autofac,asp.net-identity,owin,asp.net-web-api2,Entity Framework,Autofac,Asp.net Identity,Owin,Asp.net Web Api2,是否有人成功地使用OWIN ASP.NET标识连接了IoC,以与WebApi(或MVC)应用程序共享相同的DbContext 我希望,如果ASP.NETIdentity加载用户,它将加载到与应用程序其余部分使用的dbcontext相同的dbcontext中 (使用AutoFac作为IoC-但不介意以其他容器为例) 编辑: 2014年1月6日:更具体地说,当ASP.Net标识尝试使用DbContext时,它需要与应用程序的其余部分相同的DbContext实例。否则,它将创建同一DbContext的

是否有人成功地使用OWIN ASP.NET标识连接了IoC,以与WebApi(或MVC)应用程序共享相同的DbContext

我希望,如果ASP.NETIdentity加载用户,它将加载到与应用程序其余部分使用的dbcontext相同的dbcontext中

(使用AutoFac作为IoC-但不介意以其他容器为例)

编辑: 2014年1月6日:更具体地说,当ASP.Net标识尝试使用DbContext时,它需要与应用程序的其余部分相同的DbContext实例。否则,它将创建同一DbContext的两个实例。我已经在使用
.PerHttpApiRequest()
扩展名

我发现的问题是,在设置OWIN类时,我无法找到以下方法: 1) 将容器连接到OWIN管道上; 2) 告诉OWIN使用该容器解析它需要的类,以便它可以共享相同的作用域(即OAUthServerOptions或任何其他可能包含其他依赖项的选项)


话虽如此,如何设置ASP.Net标识来解决上述两点?

编辑:添加了根据注释实例化OWIN管道的内容

由于ASP.NET Identity使用的IdentityDdbContext继承自DbContext,因此您可以像这样从IdentityDdbContext继承创建自己的实现

public class ApplicationContext : IdentityDbContext<ApplicationUser>
{
     //additional DbSets
    public DbSet<OtherEntity> OtherEntities { get; set; } 
}

public class ApplicationUser : IdentityUser
{
    //any custom properties you want for your user.
}

public class OtherEntity
{
    [Key]
    public int Id { get; set; }
}

您找到了实现这一点的方法吗?例如,我能够使用容器获得UserManager。问题是,当初始化OWIN的类时(比如需要用户管理器工厂的OAuthServerOptions),您只能在HttpRequest之外进行解析。我看到了这个答案,但它指的是不存在的类(也许现在还不存在),您能演示OWIN初始化吗?OWIN管道如何知道在需要时如何解析ApplicationUserManager?
public static void RegisterTypes(IUnityContainer container)
{
    container.RegisterType<IUserStore<ApplicationUser>, UserStore<ApplicationUser>>(new InjectionConstructor(typeof(ApplicationContext)));
}
public class ApplicationUserManager : UserManager<ApplicationUser>
{
    public ApplicationUserManager(IUserStore<ApplicationUser> store) : base(store)
    {
    }
}
container.RegisterType<IOAuthAuthorizationServerProvider, ApplicationOAuthProvider>(new InjectionConstructor("self", typeof(ApplicationUserManager)));
static Startup()
        {
            OAuthOptions = new OAuthAuthorizationServerOptions
            {
                TokenEndpointPath = new PathString("/Token"),
                Provider = (IOAuthAuthorizationServerProvider)GlobalConfiguration.Configuration.DependencyResolver.GetService(typeof(IOAuthAuthorizationServerProvider)),
                AccessTokenExpireTimeSpan = TimeSpan.FromDays(5),
                AllowInsecureHttp = true,
                RefreshTokenProvider = new SimpleRefreshTokenProvider()
            };
        }