C# 自定义asp存储中的HttpContext问题
我正在创建一个自定义ASP应用商店,下面是所显示的问题: 邀请经理类:C# 自定义asp存储中的HttpContext问题,c#,asp.net,entity-framework,dbcontext,C#,Asp.net,Entity Framework,Dbcontext,我正在创建一个自定义ASP应用商店,下面是所显示的问题: 邀请经理类: public InvitationManager(InvitationStore store) { Store = store ?? throw new ArgumentNullException(nameof(store)); UserManager = Context.GetOwinContext().Get<ApplicationUserManager>();
public InvitationManager(InvitationStore store)
{
Store = store ?? throw new ArgumentNullException(nameof(store));
UserManager = Context.GetOwinContext().Get<ApplicationUserManager>();
}
public async void SendInvitation(Invitation inv, ApplicationUser SentTo)
{
var SentToUser = await UserManager.FindByIdAsync(SentTo.Id);
inv.SentTo = SentToUser;
var SentBy = await UserManager.FindByNameAsync(Context.User.Identity.Name);
inv.SentBy = SentBy.Id;
var not = new Notification(Referenced_user: SentTo);
inv.AssociatedNotification = not;
SentToUser.Notifications.Add(not);
await Store.CreateAsync(inv);
}
public IQueryable<Invitation> Invitations
{
get
{
var queryableStore = Store.Invitations;
if (queryableStore == null)
{
throw new NotSupportedException("Can't access database invitations");
}
return queryableStore;
}
}
protected HttpContext Context
{
get
{
return HttpContext.Current;
}
}
private ApplicationUserManager _userManager;
public ApplicationUserManager UserManager
{
get
{
if(_userManager == null)
{
_userManager = new ApplicationUserManager(new UserStore<ApplicationUser>(new ApplicationDbContext()));
}
return _userManager;
}
set
{
_userManager = value;
}
}
那我该怎么解决呢?如何使它们从相同的上下文中检索?这里仍然是如何存储dbcontext及其与UserManager的关系:
app.CreatePerOwinContext(ApplicationDbContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
{
reurn new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>()));
}
app.CreatePerOwinContext(ApplicationDbContext.Create);
app.CreatePerOwinContext(ApplicationUserManager.Create);
公共静态应用程序SerManager创建(IdentityFactoryOptions选项,IOwinContext上下文)
{
重新注册新的ApplicationUserManager(新的用户存储(context.Get());
}
我对我的邀请存储执行了完全相同的过程,我从应用程序Startup.Auth.cs文件中声明的OwinContext中获取DbContext。每次用户发出HTTP请求时都会检索到它。提供
UserManager
类的代码,这样我们就可以了解您是如何使用DbContexr
的。UserManager代码是在ASP.Net中预实现的身份代码。这不是我的,这是微软预先实现的。请参阅和评论。
app.CreatePerOwinContext(ApplicationDbContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
{
reurn new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>()));
}