0

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 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.