ASP.NET Identity - Multiple object sets per type are not supported
Solution 1
You do have two DbSet
s` 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
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
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].
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.
Be very careful when using scaffolding or even better don't use it.
Solution 4
Comment the new generated Dbset from identity model class like below
// public System.Data.Entity.DbSet<SurveyTool.Models.ApplicationUser> ApplicationUsers { get; set; }
magos
Updated on July 22, 2022Comments
-
magos almost 2 years
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 myDbContext
there aren't the same types ofDbSets
. Exception is thrown onFindAsync()
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... -
Vasil Valchev almost 9 yearsLook inside IdentityModel.cs, you will find public class ApplicationDbContext : IdentityDbContext<ApplicationUser> Inside of this context, VS sometimes adds DbSet<ApplicationUser> ApplicationUsers
-
VivekDev over 7 yearsThank you, now I know the culprit is the scaffolding that the new controller wizard did just now.
-
Leandro De Mello Fagundes over 7 yearsNice! I never used the scaffold and after used it to build a controller, everything goes wrong! Nice tip bro!
-
AminM about 7 years@VasilValchev when you inheritance from IdentityUser class it contain Users DbSet
-
Unbreakable almost 7 yearsI also got the same error, But I never added this line in IdentityModel.cs. How did it magically came there and started throwing error. I am even uploading my changes in git. How come this line came up magically there. Please guide me. P.S: commeting that line fixes the issue
-
SendETHToThisAddress over 3 yearsThis is exactly what happened to me! Note: if you generated a controller with scaffolding like this then change "ApplicationUser" to "User" in the controller