【问题标题】:ASP.net MVC 5 : Extend IdentityUser class to use My User Entity ClassASP.net MVC 5:扩展 IdentityUser 类以使用我的用户实体类
【发布时间】:2015-05-23 00:22:20
【问题描述】:

我在 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 ---

【问题讨论】:

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


    【解决方案1】:

    您是否要覆盖 OnModelCreating 类?

    示例:

    受保护的覆盖无效 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-02-15
      • 1970-01-01
      • 1970-01-01
      • 2014-04-24
      • 2011-06-25
      • 2010-09-18
      • 1970-01-01
      相关资源
      最近更新 更多