asp.net identity 2.0 unity沒有解析預設使用者儲存
嘗試使用 Unity.Mvc5 和使用 Identity 2.0 和 Identity 2.0 Samples 樣板的 MVC 5 應用程序配置 Unity 時,出現以下異常。我已閱讀此 SO Configure Unity DI for ASP.NET Identity,但我仍然不清楚我缺少什麼。我在這裡做錯了什麼?
目前類型 System.Data.Common.DbConnection 是一個抽像類,無法構造。您是否缺少類型映射?
[ResolutionFailedException: 依賴解析失敗,type = “myApp.Web.Controllers.AccountController”, name = “(none)"。異常發生時:解決時。
異常是: InvalidOperationException - 目前類型 System.Data.Common.DbConnection 是一個抽像類,無法構造。您是否缺少類型映射?
在異常發生時,容器是:
解析 myApp.Web.Controllers.AccountController,(none) 解析建構子 myApp.Web.Controllers.AccountController(myApp.Web.Models.ApplicationUserManager userManager) 的參數 “userManager” 解析 myApp.Web.Models.ApplicationUserManager,(none) 解析參數建構子 myApp.Web.Models.ApplicationUserManager 的“儲存”(Microsoft.AspNet.Identity.IUserStore
1[[myApp.Web.DAL.Profiles.ApplicationUser, myApp.Web, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] store) Resolving Microsoft.AspNet.Identity.EntityFramework.UserStore1$$ myApp.Web.DAL.Profiles.ApplicationUser $$,(none) (映射自 Microsoft.AspNet.Identity.IUserStore
1[myApp.Web.DAL.Profiles.ApplicationUser], (none)) Resolving parameter "context" of constructor Microsoft.AspNet.Identity.EntityFramework.UserStore1$$ [myApp.Web.DAL.Profiles.ApplicationUser, myApp.Web, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null $$](System.Data.Entity.DbContext context) Resolving System.Data.Entity.DbContext,(none) Resolving parameter “existingConnection” of constructor System.Data.Entity.DbContext(System.Data.Common.DbConnection existingConnection, System.Data .Entity.Infrastructure.DbCompiledModel 模型,System.Boolean contextOwnsConnection) 解析 System.Data.Common.DbConnection,(none) 帳戶控制器,因為我已對其進行了修改public AccountController(ApplicationUserManager userManager) { _userManager = userManager; } private ApplicationUserManager _userManager;我註冊的容器
container.RegisterType<ApplicationUserManager>(new HierarchicalLifetimeManager()); container.RegisterType<IUserStore<ApplicationUser>, UserStore<ApplicationUser>>(new HierarchicalLifetimeManager());
我看到您找到了解決方案,但我想我有一個更簡單的解決方案。
您正在使用實體框架,對嗎?所以你的應用程序幾乎可以肯定有一些繼承自
DbContext(可能繼承自IdentityContext<TUser>,在這種情況下又繼承自DbContext)。在預設模板中,它是 ApplicationDbContext。在您的組合根中,您可以添加
container.RegisterType<DbContext, ApplicationDbContext>(new HierarchicalLifetimeManager());(如果您的不稱為 ApplicationDbContext,則顯然編輯它)。