0

I've followed tutorials but can't seem to get this to work. What I'm trying to achieve is that I have a Client model which has an owner handling the client. That owner is any of the ApplicationUsers. Right now this is the problem that I have:

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

Here's the Client model. I added the ApplicationUserId there to make it easier to use it directly sometimes.

public class Client
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Phone { get; set; }
    public string Email { get; set; }

    // Who does this client belong to
    public virtual ApplicationUser User { get; set; }
    public string ApplicationUserId { get; set; }
}

I can add a new controller and views using scaffolding for the Client. It even builds without any issues but when I run the site I get the Multiple object sets per type error.

Scaffolding also adds Clients to ApplicationDbContext (set in IdentityModels.cs). I haven't touched any of the Identity files after VS automatically generated them so I don't believe there's an issue with them.

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

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }

    public System.Data.Entity.DbSet<MyApp.Models.Client> Clients { get; set; }
}

What's causing the Multiple object sets per type are not supported error? I've spent hours trying to figure this out. Thanks.

Hardev
  • 10,851
  • 2
  • 17
  • 17
  • Try these? http://stackoverflow.com/questions/19888576/vs-2013-controller-scaffolding-fails-for-the-applicationuser-model-multiple-obj – Steve Greene Oct 02 '15 at 19:20
  • I searched the whole project and couldn't find anywhere else the DbSet was defined but I did find the problem and solved it. I'll add the answer separately. Thanks for your help. – Hardev Oct 03 '15 at 08:39

1 Answers1

0

I found many suggestions how to fix the issue and they may apply to some cases but at least for my project in Visual Studio 2015 the issue is that the scaffolding does a bad job and in the generated Controller instead of using "Users" that I defined in the Client model the code has "ApplicationUsers" which I guess is causing the conflict. I solved this by changing the controllercode from e.g.

ViewBag.ApplicationUserId = new SelectList(db.ApplicationUsers, "Id", "Email", client.ApplicationUserId);

to

ViewBag.ApplicationUserId = new SelectList(db.Users, "Id", "Email", client.ApplicationUserId);

Basically everything in the controller should be db.Users instead of the automatically generated db.ApplicationUsers.

Also make sure that the ApplicationDbContext in IdentityModels.cs doesn't contain additional Users or ApplicationUsers DbSets. They shouldn't be there. I didn't have this problem but I guess people with VS2013 have seen this happen.

Hardev
  • 10,851
  • 2
  • 17
  • 17