Question

After updating to latest Asp.Net Identity (from 1.0 to 2.0) i got an exception on CRUD of a user from DB:

The entity type User is not part of the model for the current context

// for example here:

var manager = Container.Resolve<UserManager<User>>();
        IdentityResult result = manager.Create(user, "Test passwd");


public class User : IdentityUser
{
    // Some properties
}


public class AppDbContext : IdentityDbContext<User>
{
    static AppDbContext()
    {
        // Without this line, EF6 will break.
        Type type = typeof (SqlProviderServices);
    }

    public AppDbContext()
        : this("DBName")
    {
    }

    public AppDbContext(string connectionString)
        : base(connectionString)
    {
    }

    ...
}


protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();

        modelBuilder.Entity<User>()
            .HasMany(c => c.SkippedTopics)
            .WithMany(i => i.Users)
            .Map(t =>
                t.MapLeftKey("User_Id")
                    .MapRightKey("Topic_Id")
                    .ToTable("UserSkippedTopics"));
    ...
}

Before the update the code worked correctly. Any ideas?


UPDATE

The problem was in Unity configuration for UserManager types.

Fix: i created empty AppUserStore: UserStore and registered it as IUserStore:

public class AppUserStore : UserStore<User>
{
    public AppUserStore(AppDbContext context) : base(context)
    {
    }
}


...

public static void RegisterTypes(IUnityContainer container)
{
    ...

    _container.RegisterType<AppDbContext, AppDbContext>(new InjectionConstructor());
    _container.RegisterType<IUserStore<User>, AppUserStore>();
}
Was it helpful?

Solution

shouldn't there be UserStore somewhere? I mean looks like you're using Unity for DI which I've never used so I don't know how it works but normally to instantiate UserManager you do something like this:

var manager = new UserManager<User>(new UserStore<User>(new AppDbContext()));

OTHER TIPS

The same thing happened to me and it sucks. You need to enable-immigration and update your database from the Nuget manager console. Check out this blog. Link is here

This just happened to me also when upgrading from Identity 1.0 to 2.0. The identity code scaffold by Visual Studio (such as the AccountController, ApplicationOAuthProvider, Startup.Auth.cs, etc) was generated when I created the project in Asp.Net Identity 1.0. It seems that Visual Studio had an update and now generates slightly different code for Identity 2.0.

The solution for me was to generate a new web app and copy over all the (new) generated code into the project I just upgraded, replacing the old generated code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top