9

I am stuck at an odd issue.

I am learning MVC 5 and almost everything is created by the built-in template. I only added a class History

public class History
{
    [Key]
    public DateTime Date { get; set; }

    public virtual ApplicationUser User { get; set; }

    public string ItemName { get; set; }

}

And inside the built-in ApplicationUser:

  public class ApplicationUser : IdentityUser
    {
        public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
        {
            // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
            var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
            // Add custom user claims here
            return userIdentity;
        }

        public virtual ICollection<History> Histories { get; set; }
    }

Here is the error message:

 Multiple object sets per type are not supported. The object sets 'ApplicationUsers' and 'Users' can both contain instances of type 'MyOnlineShopping.Models.ApplicationUser'.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

    Exception Details: System.InvalidOperationException: Multiple object sets per type are not supported. The object sets 'ApplicationUsers' and 'Users' can both contain instances of type 'MyOnlineShopping.Models.ApplicationUser'.

    Source Error: 


    Line 124:            {
    Line 125:                var user = new ApplicationUser() { UserName = model.Email, Email = model.Email };
    Line 126:                IdentityResult result = await UserManager.CreateAsync(user, model.Password);
    Line 127:                if (result.Succeeded)
    Line 128:                {
Source File: f:\Workplace\MyOnlineShopping\MyOnlineShopping\Controllers\AccountController.cs    Line: 126 
Franva
  • 6,565
  • 23
  • 79
  • 144
  • I have run into the same problem. In my case everything was working fine until I added an action to my controller, then added a view to the action. I've removed the action and the view but still get the error. (Those are the only two things that I am aware of adding) – John S Jun 11 '14 at 21:36
  • 1
    hi @JohnS I think I found the "superficial answer" for this one. I discovered that it seems that the EntityFramework automtically added a collection of IdentityUser under ApplicationUser . I removed that, problem gone. Hope it helps for you. – Franva Jun 12 '14 at 08:35

3 Answers3

24

The above is just a work around, There are many answers to this same problem all around SO.

Here

Here

and Here

Look inside IdentityModel.cs, you will find

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>

Inside of this context, VS sometimes adds DbSet<ApplicationUser> ApplicationUsers

THIS IS A DUPLICATE

Inside of the IdentityDbContext class is a DbSet User, when you subclass from IdentityContext, inheritance converts that line to DbSet<ApplicationUser> User.

The fix? Remove the DbSet<ApplicationUser> User from public class ApplicationDbContext : IdentityDbContext<ApplicationUser>

Community
  • 1
  • 1
Jake Garrison
  • 403
  • 4
  • 10
  • Why are you bolding "THIS IS A DUPlICATE"? – Russia Must Remove Putin Oct 18 '14 at 00:53
  • To add emphasis to the fact that the duplicate DbSet is located at that location – Jake Garrison Oct 19 '14 at 01:17
  • Hi Jake, thx a lot. I can still remember a little bit the code even it's 4 months ago. I did remember the code you mentioned in my project. I will give it a try. thx – Franva Oct 21 '14 at 04:24
  • Your welcome! Hopefully anyone else who searches this question finds my answer helpful as well. Thank you – Jake Garrison Oct 22 '14 at 06:38
  • You just saved my remaining hair from being pulled out. This cropped up after I added some custom properties to my `ApplicationUser` class and gave no clues as to way. Thanks! :) – iCollect.it Ltd Mar 18 '15 at 15:59
  • I forgot about every time I create a list view with an ApplicationUser as the model using the scaffolding (add view), how it adds that dbset to the public class you mentioned in your post @Jake Garrison, it kept me from making a hole in the wall with my head tonight. – nocturns2 Dec 08 '16 at 08:27
0

This error happens when your ApplicationDbContext class changed. just do this thing into your Models Folder and go to the IdentityModels.cs class and remove the Last line

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

This line is generated automatically by scaffold.

Saurabh Chauhan
  • 3,161
  • 2
  • 19
  • 46
Rahul Jograna
  • 856
  • 10
  • 9
-2

I ended up renaming ApplicationUser to User, rescaffolding and everything magically started working.

Looks like the templates are a bit out of wack.

John S
  • 7,909
  • 21
  • 77
  • 145
  • Hi John, I did rename ApplicationUser to User(didn't work either), but didn't rescafforlding. How did you do the rescafforlding? – Franva Jun 13 '14 at 01:22
  • 1
    I triggered rescaffolding by creating another view from the action that was using the User class. I'm sure there is a better or more direct way but I can't seem to find that menu option so I went this route. – John S Jun 13 '14 at 15:31