VS 2013 Controller Scaffolding Fails for the ApplicationUser Model (Multiple object sets per type are not supported)

夙愿已清 提交于 2019-12-17 17:56:10

问题


In a VS 2013 RTM, MVC 5 project with EF 6, I tried to scaffold a controller based on the ApplicationUser (default with individual accounts authentication). Both ApplicationUser and IdentityUser are mapped to a Users table. The wizard opens the context file for editing and tries to add a new db set for ApplicationUser (ApplicationUsers) and then fails with this error:

Unable to retrieve metadata for ApplicationUser. Multiple object sets per type are not supported. The object sets ApplicationUsers and Users can both contain instances of type ApplicationUser The solution does not have any reference to, or instance of ApplicationUsers.

Is this a known issue? Can the scaffolding be run using command line with options (from PMC)? Note: scaffolding also adds an extra db set to the context class if I specify a model that references ApplicationUser (the app works if I remove it and fix references in the generate controller).


回答1:


Wow. I'm really surprise that no one actually got to the root of this, and instead, are just recommending workarounds.

IdentityDbContext already contains a property:

`public virtual IDbSet<TUser> Users { get; set; }

When you subclass IdentityDbContext to create your own application-specific context, you must specify what class satisfies the TUser generic. The default is:

public ApplicationDbContext : IdentityDbContext<ApplicationUser>

Which then means that you functionally have a property already via inheritance in the form of:

public IDbSet<ApplicationUser> Users { get; set; }

If you then add another property to your application-specific context such as:

public DbSet<ApplicationUser> ApplicationUsers { get; set; }

You now have the same entity tracked by two DbSets, and you get that error. The solution? Simply don't add your own DbSet for ApplicationUser. There's no need to rename or override anything.




回答2:


Short-Version: Rename your ApplicationUser class to User.

I've been running into this problem for about a month with absolutely no luck...until now!

Initially, I thought it was a preview issue, but after persisting into the RTM along with the latest libraries, I became incredibly annoyed, since this problem persisted into Migrations too.

However, IdentityDbContext, according to the error message, seems to be creating two DbSets: ApplicationUsers and Users. We only want Users when looking at the source code:

public class IdentityDbContext<TUser> : DbContext where TUser : Microsoft.AspNet.Identity.EntityFramework.IdentityUser
{
    ...
    public virtual IDbSet<TUser> Users { get; set; }
    ...
}

From this, we (and the scaffolding engine, and the migrations engine) should only see "Users", not "ApplicationUsers".

To rectify this situation, you will need to adjust your application class to account for this rather strange error. Simply rename your ApplicationUser class to User:

using Microsoft.AspNet.Identity.EntityFramework 
...
public class ApplicationUser : IdentityUser
{
    Your Stuff
}

To:

using Microsoft.AspNet.Identity.EntityFramework 
...
public class User: IdentityUser
{
    Your Stuff
}

Attempt to Scaffold again. If you receive another error along the lines of the class cannot be found, save your project, close VS2013, re-open VS2013, load the project, re-build the project, and finally attempt to scaffold. The IdentityDBContext should no longer be creating a dummy "ApplicationUsers" DBSet object causing both Entity Migrations and Scaffolding to issue these errors.

Hope this helps!

P.S. Any mapping done ought not to affect this problem, so you should be able to still map to the same table if you wish to.

EDIT: If you receive further problems, undo the rename. I ran into some problems (more scaffolding and query errors), and after I went back to ApplicationUser, those problems disappeared and the problem above did not re-occur. Just a heads up.




回答3:


Read the above problems en solutions. My error text was:

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

I suspect the error was created when I was playing around and scaffolded the model: ApplicationUser in a new controller.

Solved it by removing the below from : ApplicationDbContext.cs

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

No Other changes where made to solve the problem. I hope this helps someone.




回答4:


When you use scaffolding to generate control, vs will auto insert 1 line to your db context

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

Just delete that line, and in your controller. change db.ApplicationUsers to db.Users




回答5:


I spent more than one day solving this issue... :((( Renaming class is only workaround. The correct solution is to remove this line from ApplicationContext (IdentityModels.cs). I dont know when it was inserted into my code.

System.Data.Entity.DbSet ApplicationUsers { get; set; }

Users are managed somewhere inside, there should be USER DEFINED TABLES HERE ONLY. It means that this wrong line is the second appearance, and this is the reason for "Multiple object sets" error message.

After deleting this line and compiling, I got several error messages in my automatically generated view code, as it already referenced the wrong declaration. You can recreate the view or fix the code manually, just simply rename wrong references to ApplicationUser to correct User (correctly defined somewhere inside, not in IdentityModels.cs)

During trying to find the solution I also corrupted my migrations so I had to start migration of data structures to DB from scratch.

Now I am completely recovered from this issue and the almost-ROOT issue is clear. Unfortunatelly the root issue is probably a bug in VS2013, that creates that wrong additional line.

But anyway, keep smiling. Facing such bugs is nothing compared to programming in Java :)




回答6:


Here is the simplest solution. When you add/scaffold a view (list) based on ApplicationUser as the model, VS2013 ADDS the following to the IdentityModels.vb or .cs file.:

Public Property ApplicationUsers As System.Data.Entity.DbSet(Of ApplicationUser)

Just remove this property and the problem goes away.




回答7:


If you are trying to create an ApplicationUsersController please follow these steps.

  1. Delete this line from IdentityModels.cs

    public System.Data.Entity.DbSet<Project.Models.ApplicationUser> ApplicationUsers { get; set; }
    
  2. Build the project

    control + shift + b
    
  3. Generate the controller

    Right click on the 'Controllers' folder.
    Add > Controller
    MVC Controller with views, using Entity Framework
    Model Class: ApplicationUser
    Add
    
  4. Go back to IdentityModels.cs and delete this line AGAIN

    public System.Data.Entity.DbSet<Project.Models.ApplicationUser> ApplicationUsers { get; set; }
    
  5. Build the project

    control + shift + b
    
  6. Change the database calls to from ApplicationUsers to Users in ApplicationUsersController.cs

    control + f to bring up 'Find and Replace'
    Click 'Replace in files'
    Find what: db.ApplicationUsers
    Replace with: db.Users
    Replace All
    
  7. Press play and cross fingers :)




回答8:


What you can also do: Create an empty controller, and add the code for the DataContext yourself

    protected ApplicationDbContext db { get; private set; }

    public HomeController() : this(new ApplicationDbContext())
    {
    }

    public HomeController(ApplicationDbContext db)
    {
        this.db = db;
    }

Then create your methods like Index, Create and so on, and create the views with right-clicking and "Add view..." for each. Scaffold your views with the list, create, whatever template that is appropriate and choose the ApplicationUser as an model.

Important: Delete the entry in "Data context class", or you will get a similar error again. But if you leave the "Data context class" empty, the scaffolding of the view will work out fine.




回答9:


I fixed problem by removing DbSet from context and then changing references in controller from ApplicationUsers to Users. It worked - but now i see no point in scaffolding users. To much things has to by maintained on top level and it just does not work right. Now i know that view models and repository are the way I want to go.



来源:https://stackoverflow.com/questions/19888576/vs-2013-controller-scaffolding-fails-for-the-applicationuser-model-multiple-obj

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!