Asp.net 身份 DbContext/存储库问题
Asp.net Identity DbContext / Repository Issue
我在我的 MVC 应用程序中使用 Asp.Net 身份。我可以看到它有自己的 ApplicationDbContext - 尽管它连接到与我在其他地方使用的自己的 DbContext 相同的 SQL 数据库。
所以我正在尝试通过我自己在 AccountController 中的代码访问我自己的一些数据 - 我认为它似乎不起作用是因为它认为哪个 DBContext 是活动的有些混淆?
我的代码:
public class AccountController : Controller
{
private ApplicationSignInManager _signInManager;
private ApplicationUserManager _userManager;
private PostageManager postmgr;
public AccountController()
{
}
public AccountController(ApplicationUserManager userManager, ApplicationSignInManager signInManager, PostageManager _postmgr)
{
UserManager = userManager;
SignInManager = signInManager;
postmgr = _postmgr;
}
public ApplicationSignInManager SignInManager
{
get
{
return _signInManager ?? HttpContext.GetOwinContext().Get<ApplicationSignInManager>();
}
private set
{
_signInManager = value;
}
}
public ApplicationUserManager UserManager
{
get
{
return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
}
private set
{
_userManager = value;
}
}
// GET: /Account/Register
[AllowAnonymous]
public ActionResult Register()
{
//create select list items for countries drop down
List<SelectListItem> countries;
countries = postmgr.GetCountries().Select(item => new SelectListItem
{
Value = item.Country,
Text = item.Country
}).ToList();
countries.Insert(0, new SelectListItem { Value = string.Empty, Text = "Select delivery country or region...", Selected = true });
RegisterViewModel mode = new RegisterViewModel
{
Countries = countries
};
return View();
}
}
}
PostageManager 只是一个 class,位于我的 DAL 之上以获取一些数据(使用存储库模式)- 我只是使用一种传递方法来获取国家列表,并使用它与我在其他控制器中使用的方式完全相同,效果很好。 class 下面是我的存储库代码,它链接到我的默认连接字符串 (DBContext)。它在下一行出现空引用异常,我认为 postmgr 是空的:
countries = postmgr.GetCountries().Select(item => new SelectListItem
为了在我自己的控制器中访问身份数据,我做了以下操作:
public BasketController(BasketManager _mgr, PostageManager _postmgr, ProductManager _prodmgr)
{
mgr = _mgr;
postmgr = _postmgr;
prodmgr = _prodmgr;
shopper = Cart.GetShopperId();
this.applicationDbContext = new ApplicationDbContext();
this.userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(this.applicationDbContext));
}
protected ApplicationDbContext applicationDbContext { get; set; }
protected UserManager<ApplicationUser> userManager { get; set; }
据我所知,它指向身份代码以使用正确的 DbContext - 我在我的 AccountController 中查看了反向执行此操作,但无法理解。
我基本上只是希望能够使用我自己的代码从身份控制器中获取我自己的数据,以帮助将额外数据等传递给视图。
我可能错了,但很可能 postmgr 字段没有从构造函数初始化,这就是为什么你有这个错误。
解释:
默认情况下Asp将尝试通过不带参数的构造函数创建控制器实例。如果 Asp 找不到不带参数的构造函数,它将尝试调用带参数的构造函数,但要使其成为可能,您必须在应用程序中配置 IoC。由于您的控制器具有不带参数的构造函数,它将由 Asp 选择。所以所有 3 个字段都是空的。
但是在属性 SignInManager 和 UserManager 中,您尝试从字段或 OwinContext 中获取值。由于字段为空,您的代码将从 OwinContext 中获取值。 OwinContext 是一个非常复杂和智能的工具,它根据 Startup.Auth.cs 文件或 App_Start 文件夹下的任何其他文件中提供的配置自动创建其上下文。
我想我已经弄明白了 - 将以下内容添加到我的 NinjectControllerFactory 中:
ninjectKernel.Bind<IAuthenticationManager>().ToMethod(c => HttpContext.Current.GetOwinContext().Authentication); //.InRequestScope();
ninjectKernel.Bind<IUserStore<ApplicationUser>>().To<UserStore<ApplicationUser>>();
ninjectKernel.Bind<UserManager<ApplicationUser>>().ToSelf();
ninjectKernel.Bind<IRoleStore<IdentityRole, string>>().To<RoleStore<IdentityRole, string, IdentityUserRole>>();
ninjectKernel.Bind<RoleManager<IdentityRole>>().ToSelf();
并将我的构造函数更改为:
public AccountController(PostageManager _postmgr)
{
postmgr = _postmgr;
}
我在我的 MVC 应用程序中使用 Asp.Net 身份。我可以看到它有自己的 ApplicationDbContext - 尽管它连接到与我在其他地方使用的自己的 DbContext 相同的 SQL 数据库。
所以我正在尝试通过我自己在 AccountController 中的代码访问我自己的一些数据 - 我认为它似乎不起作用是因为它认为哪个 DBContext 是活动的有些混淆?
我的代码:
public class AccountController : Controller
{
private ApplicationSignInManager _signInManager;
private ApplicationUserManager _userManager;
private PostageManager postmgr;
public AccountController()
{
}
public AccountController(ApplicationUserManager userManager, ApplicationSignInManager signInManager, PostageManager _postmgr)
{
UserManager = userManager;
SignInManager = signInManager;
postmgr = _postmgr;
}
public ApplicationSignInManager SignInManager
{
get
{
return _signInManager ?? HttpContext.GetOwinContext().Get<ApplicationSignInManager>();
}
private set
{
_signInManager = value;
}
}
public ApplicationUserManager UserManager
{
get
{
return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
}
private set
{
_userManager = value;
}
}
// GET: /Account/Register
[AllowAnonymous]
public ActionResult Register()
{
//create select list items for countries drop down
List<SelectListItem> countries;
countries = postmgr.GetCountries().Select(item => new SelectListItem
{
Value = item.Country,
Text = item.Country
}).ToList();
countries.Insert(0, new SelectListItem { Value = string.Empty, Text = "Select delivery country or region...", Selected = true });
RegisterViewModel mode = new RegisterViewModel
{
Countries = countries
};
return View();
}
}
}
PostageManager 只是一个 class,位于我的 DAL 之上以获取一些数据(使用存储库模式)- 我只是使用一种传递方法来获取国家列表,并使用它与我在其他控制器中使用的方式完全相同,效果很好。 class 下面是我的存储库代码,它链接到我的默认连接字符串 (DBContext)。它在下一行出现空引用异常,我认为 postmgr 是空的:
countries = postmgr.GetCountries().Select(item => new SelectListItem
为了在我自己的控制器中访问身份数据,我做了以下操作:
public BasketController(BasketManager _mgr, PostageManager _postmgr, ProductManager _prodmgr)
{
mgr = _mgr;
postmgr = _postmgr;
prodmgr = _prodmgr;
shopper = Cart.GetShopperId();
this.applicationDbContext = new ApplicationDbContext();
this.userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(this.applicationDbContext));
}
protected ApplicationDbContext applicationDbContext { get; set; }
protected UserManager<ApplicationUser> userManager { get; set; }
据我所知,它指向身份代码以使用正确的 DbContext - 我在我的 AccountController 中查看了反向执行此操作,但无法理解。
我基本上只是希望能够使用我自己的代码从身份控制器中获取我自己的数据,以帮助将额外数据等传递给视图。
我可能错了,但很可能 postmgr 字段没有从构造函数初始化,这就是为什么你有这个错误。 解释:
默认情况下Asp将尝试通过不带参数的构造函数创建控制器实例。如果 Asp 找不到不带参数的构造函数,它将尝试调用带参数的构造函数,但要使其成为可能,您必须在应用程序中配置 IoC。由于您的控制器具有不带参数的构造函数,它将由 Asp 选择。所以所有 3 个字段都是空的。
但是在属性 SignInManager 和 UserManager 中,您尝试从字段或 OwinContext 中获取值。由于字段为空,您的代码将从 OwinContext 中获取值。 OwinContext 是一个非常复杂和智能的工具,它根据 Startup.Auth.cs 文件或 App_Start 文件夹下的任何其他文件中提供的配置自动创建其上下文。
我想我已经弄明白了 - 将以下内容添加到我的 NinjectControllerFactory 中:
ninjectKernel.Bind<IAuthenticationManager>().ToMethod(c => HttpContext.Current.GetOwinContext().Authentication); //.InRequestScope();
ninjectKernel.Bind<IUserStore<ApplicationUser>>().To<UserStore<ApplicationUser>>();
ninjectKernel.Bind<UserManager<ApplicationUser>>().ToSelf();
ninjectKernel.Bind<IRoleStore<IdentityRole, string>>().To<RoleStore<IdentityRole, string, IdentityUserRole>>();
ninjectKernel.Bind<RoleManager<IdentityRole>>().ToSelf();
并将我的构造函数更改为:
public AccountController(PostageManager _postmgr)
{
postmgr = _postmgr;
}