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

asp.net asp.net-mvc-4 entity-framework

Question

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 
1
9
5/29/2014 2:18:29 PM

Accepted Answer

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

Looks like the templates are a bit out of wack.

-2
6/12/2014 5:58:48 PM

Popular Answer

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>



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow