在尝试使用Unity.Mvc5和使用Identity 2.0和Identity 2.0样本样板的MVC5应用程序配置Unity时,我得到了以下异常。我已经读过这篇文章了,所以Configure Unity DI for ASP.NET Identity,我仍然不清楚我错过了什么。我在这里做错了什么?
当前类型System.Data.Common.DbConnection是一个抽象类,无法构造。您是否缺少类型映射?
[ResolutionFailedException:依赖项解析失败,类型= "myApp.Web.Controllers.AccountController",名称= "(none)“。解析时出现异常。
例外情况是: InvalidOperationException -当前类型System.Data.Common.DbConnection是一个抽象类,无法构造。您是否缺少类型映射?
在发生异常时,容器是:
解析存储,(none)解析构造函数myApp.Web.Controllers.AccountController(myApp.Web.Models.ApplicationUserManager myApp.Web.Models.ApplicationUserManager(Microsoft.AspNet.Identity.IUserStore1[[myApp.Web.DAL.Profiles.ApplicationUser, myApp.Web, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] store) Resolving Microsoft.AspNet.Identity.EntityFramework.UserStore1myApp.Web.DAL.Profiles.ApplicationUser,的参数userManager)解析myApp.Web.Models.ApplicationUserManager,(none)解析构造函数myApp.Web.Models.ApplicationUserManager(Microsoft.AspNet.Identity.IUserStore1[[myApp.Web.DAL.Profiles.ApplicationUser, myApp.Web, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] store) Resolving Microsoft.AspNet.Identity.EntityFramework.UserStore1myApp.Web.DAL.Profiles.ApplicationUser,的参数myApp.Web.Models.ApplicationUserManager (none) (从none解析System.Data.Entity.DbContext映射而来,(无)解析构造函数System.Data.Entity.DbContext(System.Data.Common.DbConnection模型的参数existingConnection,System.Data.Entity.Infrastructure.DbCompiledModel existingConnection,System.Boolean contextOwnsConnection)解析System.Data.Common.DbConnection,(无)
帐户控制器,因为我已经修改了它
public AccountController(ApplicationUserManager userManager)
{
_userManager = userManager;
}
private ApplicationUserManager _userManager;我已注册的容器
container.RegisterType<ApplicationUserManager>(new HierarchicalLifetimeManager());
container.RegisterType<IUserStore<ApplicationUser>, UserStore<ApplicationUser>>(new HierarchicalLifetimeManager());发布于 2014-04-23 01:22:30
好了,我想通了。我认为。我创建了自己的userstore类,并将其插入其中。这在一定程度上有所帮助。http://odetocode.com/blogs/scott/archive/2014/01/20/implementing-asp-net-identity.aspx
unity配置
container.RegisterType<ApplicationDbContext>(new HierarchicalLifetimeManager());
container.RegisterType<IUserStore<ApplicationUser>, bcUserStore>(new HierarchicalLifetimeManager());用户管理器
public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
{
var manager = new ApplicationUserManager(new bcUserStore(context.Get<ApplicationDbContext>()));
// other code that's not relevant
}用户存储
public class bcUserStore : IUserStore<ApplicationUser>
{
private IDbSet<ApplicationUser> _users;
private ApplicationDbContext _context;
public bcUserStore(ApplicationDbContext context)
{
_users = context.Users;
_context = context;
}
public System.Threading.Tasks.Task CreateAsync(ApplicationUser user)
{
user.Id = Guid.NewGuid().ToString();
_users.Add(user);
return _context.SaveChangesAsync();
}
public System.Threading.Tasks.Task DeleteAsync(ApplicationUser user)
{
_users.Remove(user);
return _context.SaveChangesAsync();
}
public System.Threading.Tasks.Task<ApplicationUser> FindByIdAsync(string userId)
{
return _users.FirstOrDefaultAsync(x => x.Id == userId);
}
public System.Threading.Tasks.Task<ApplicationUser> FindByNameAsync(string userName)
{
return _users.FirstOrDefaultAsync(x => x.UserName == userName);
}
public System.Threading.Tasks.Task UpdateAsync(ApplicationUser user)
{
var current = _users.Find(user.Id);
_context.Entry<ApplicationUser>(current).CurrentValues.SetValues(user);
return _context.SaveChangesAsync();
}
public void Dispose()
{
_users = null;
_context.Dispose();
}
}https://stackoverflow.com/questions/23210828
复制相似问题