ASP.net MVC 5 : Extend IdentityUser class to use My User Entity Class

asp.net-identity asp.net-mvc-5 entity-framework-6

Question

My UserManager Class in Asp.net MVC 5 is of below type

public UserManager<ApplicationUser<MyUser>> UserManager { get; private set; }

At run time type is ;

{Microsoft.AspNet.Identity.UserManager<app.Models.ApplicationUser<MyCustom.DataAccessLayer.MyUser>>}

When I am executing findAsync to authenticate.

var user = await UserManager.FindAsync(model.UserName, model.Password);

Then exception System.InvalidOperationException is thrown. Because it is not looking for MyUser entity, its look for ApplicationUser1 entity.

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
5/22/2015 10:18:25 AM

Popular Answer

Are you overriding OnModelCreating Class?

Sample:

protected override void OnModelCreating(DbModelBuilder modelBuilder) {

     base.OnModelCreating(modelBuilder);

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

}

you should let entity framework know that for MyUser Entity ApplicationUser entity should be used.

This link was very helpful for me : https://www.youtube.com/watch?v=elfqejow5hM

1
5/22/2015 4:22:20 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow