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

后端 未结 8 2392
死守一世寂寞
死守一世寂寞 2020-12-08 11:07

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 Applic

相关标签:
8条回答
  • 2020-12-08 11:29

    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.

    0 讨论(0)
  • 2020-12-08 11:30

    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.

    0 讨论(0)
  • 2020-12-08 11:31

    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.

    0 讨论(0)
  • 2020-12-08 11:33

    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 :)

    0 讨论(0)
  • 2020-12-08 11:34

    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.

    0 讨论(0)
  • 2020-12-08 11:46

    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.

    0 讨论(0)
提交回复
热议问题