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

后端 未结 8 2393
死守一世寂寞
死守一世寂寞 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:50

    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

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

    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.

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