ASP.NET Identity - Multiple object sets per type are not supported

C#asp.netasp.net MvcEntity Frameworkasp.net Identity

C# Problem Overview


I got an error using ASP.NET Identity in my app.

> Multiple object sets per type are not supported. The object sets > 'Identity Users' and 'Users' can both contain instances of type > 'Recommendation Platform.Models.ApplicationUser'.

I saw a few questions about this error in StackOverflow. All indicate on two DbSet objects of the same type. But in my DbContext there aren't the same types of DbSets. Exception is thrown on FindAsync() method during logging in.

if (ModelState.IsValid)
    var user = await UserManager.FindAsync(model.UserName, model.Password);
    if (user != null && user.IsConfirmed)
    {

The problem is I don't have two DbSets of the same type. My Contexts look like this:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("DefaultConnection")
    {
    }

    public System.Data.Entity.DbSet<RecommendationPlatform.Models.ApplicationUser> IdentityUsers { get; set; }

}

and

public class RecContext : DbContext
{
    public RecContext()
        : base("RecConnection")
    {
        Database.SetInitializer<RecContext>(new DropCreateDatabaseIfModelChanges<RecContext>());
    }

    public DbSet<Recommendation> Recommendations { get; set; }
    public DbSet<Geolocation> Geolocations { get; set; }
    public DbSet<Faq> Faqs { get; set; }
    public DbSet<IndexText> IndexTexts { get; set; }
}

What could cause this problem? Maybe something connected with in-built ASP.NET Identity functionalities? Anyway, what is Users type? I don't have it in my app...

C# Solutions


Solution 1 - C#

You do have two DbSets` of the same type.

IdentityDbContext<T> itself contains Users property declared as:

public DbSet<T> Users { get; set; }

You're declaring second one in your class.

Solution 2 - C#

review this file "ApplicationDbContext.cs", remove the line, generated automatically by scaffold last, should be like this:

public System.Data.Entity.DbSet<Manager.Models.ApplicationUser> IdentityUsers { get; set; }

Solution 3 - C#

This issue can arise from using scaffolding to create a View. You probably did something like this: View > Add > New Scaffold Item... > MVC 5 View > [Model class: ApplicationUser].

Add View Screenshot

The scaffolding wizard added a new line of code in your ApplicationDbContext class.

public System.Data.Entity.DbSet<RecommendationPlatform.Models.ApplicationUser> IdentityUsers { get; set; }

Now you have two DbSet properties of the same type which not only causes an exeptions to be thrown in the FindAsync() method but also when you try to use code-first migrations.

Exception when using code-first migrations

Be very careful when using scaffolding or even better don't use it.

Solution 4 - C#

Comment the new generated Dbset from identity model class like below

// public System.Data.Entity.DbSet<SurveyTool.Models.ApplicationUser> ApplicationUsers { get; set; }

Solution 5 - C#

Whenever I see this problem, I always double check the DbSet. - ESPECIALLY if you are using another language for Visual Studio.

For us who use other language on VS, always double check because the program doesn´t create controllers or models with the exact name. perhaps this should be a thread.. or there is one already and I missed it.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmagosView Question on Stackoverflow
Solution 1 - C#MarcinJuraszekView Answer on Stackoverflow
Solution 2 - C#Héctor FuentesView Answer on Stackoverflow
Solution 3 - C#Boško BezikView Answer on Stackoverflow
Solution 4 - C#Wonde_ManView Answer on Stackoverflow
Solution 5 - C#MateusLView Answer on Stackoverflow