我的UserManager类在Asp.net MVC 5中的类型如下
public UserManager<ApplicationUser<MyUser>> UserManager { get; private set; }
运行时类型为;
{Microsoft.AspNet.Identity.UserManager<app.Models.ApplicationUser<MyCustom.DataAccessLayer.MyUser>>}
当我执行findAsync验证。
var user = await UserManager.FindAsync(model.UserName, model.Password);
Then exception System。抛出InvalidOperationException。因为它不是寻找MyUser实体,而是寻找ApplicationUser1实体。
The entity type ApplicationUser1 is not part of the model for the current context.
at System.Data.Entity.Internal.InternalContext.UpdateEntitySetMappingsForType(Type entityType)
at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
at System.Data.Entity.Internal.Linq.InternalSet1.Initialize()
at System.Data.Entity.Internal.Linq.InternalSet1.get_InternalContext()
at System.Data.Entity.Infrastructure.DbQuery1.System.Linq.IQueryable.get_Provider()
at System.Linq.Queryable.Where[TSource](IQueryable1 source, Expression1 predicate)
at Microsoft.AspNet.Identity.EntityFramework.UserStore1.FindByNameAsync(String userName)
at Microsoft.AspNet.Identity.UserManager1.<FindByNameAsync>d__d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter1.GetResult()
at Microsoft.AspNet.Identity.UserManager1.<FindAsync>d__15.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
是否重写OnModelCreating类?
示例:protected override void OnModelCreating(DbModelBuilder modelBuilder){
base.OnModelCreating(modelBuilder);
//Map Entities to their tables.
modelBuilder.Entity<ApplicationUser>().ToTable("MyUser");
}
你应该让实体框架知道MyUser实体应该使用ApplicationUser实体。
这个链接对我很有帮助:https://www.youtube.com/watch?v=elfqejow5hM