ASP.net MVC 5:扩展I​​dentityUser类以使用我的用户实体类

时间:2015-05-22 10:14:27

标签: asp.net-mvc-5 entity-framework-6 asp.net-identity

我在Asp.net MVC 5中的UserManager类属于

类型
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);

然后抛出异常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 ---

1 个答案:

答案 0 :(得分:1)

你是否重写OnModelCreating Class?

样品:

protected override void OnModelCreating(DbModelBuilder modelBuilder) {

     base.OnModelCreating(modelBuilder);

     //Map Entities to their tables.
     modelBuilder.Entity<ApplicationUser>().ToTable("MyUser");

}

你应该让实体框架知道应该使用MyUser Entity ApplicationUser实体。

此链接对我非常有帮助:https://www.youtube.com/watch?v=elfqejow5hM