locked
Cannot create a DbSet for 'ApplicationUser' because this type is not included in the model for the context RRS feed

  • Question

  • User1280950372 posted

    I reverse-engineered my database with the scaffold command in the NuGet Package Manager Console (Scaffold-DbContext "[server info]" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models –Context AppDbContext) then deleted the Identity elements from the resulting context. The problem is, when I run the program and try to register or log in a user, I get the error in the subject line above. It doesn't make sense to manually put ApplicationUser in the context file (which I think is what the error is asking) since it's not a table. (I looked at some colleagues' projects and they don't have ApplicationUser in their contexts.) In the future, if I do a migration, it will create a table by the name of ApplicationUser. Any suggestions?

    Thursday, June 22, 2017 6:18 PM

Answers

  • User1280950372 posted

    I found a coworker who had had a similar problem and helped me resolve it in the following way. It's similar to Cathy's suggested reference https://stackoverflow.com/questions/39262222/invalidoperationexception-when-registering-a-new-user-with-asp-net-core-identit but that didn't go far enough. Some posts  I've read advise something similar to what I discovered but seemed to indicate that it shouldn't be necessary to create two contexts, but since it worked for me, as far as I'm concerned it was necessary. I created my context by reverse-engineering the database (a whole subject unto itself) then, among other things, made sure it had this signature:

    public partial class [name of your context class] : DbContext
    
    //constructor
    public [context name](DbContextOptions<[context name]> options) : base(options) { }
    protected override void OnModelCreating(ModelBuilder builder)
    {
    //DbSets and classes generated by reverse-engineering
    }

    Next, I created a separate context class for the Identity elements. Do this by creating a class in the same folder as your first context, decide on a name for the new context, and replace the code in the class with the following:

    using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
    using Microsoft.EntityFrameworkCore;
    using [your project name].Models;
    
    namespace [your project name].[your namespace]
    {
        public class [your Identity context name] : IdentityDbContext<ApplicationUser>
    	{
    		public [Identity context](DbContextOptions<[Identity context]> options)
                : base(options)
            {
    		}
    
    		protected override void OnModelCreating(ModelBuilder builder)
    		{
    			base.OnModelCreating(builder);
    			// Customize the ASP.NET Identity model and override the defaults if needed.
    			// For example, you can rename the ASP.NET Identity table names and more.
    			// Add your customizations after calling base.OnModelCreating(builder);
    		}
    	}
    }
    

    Modify your Startup file under the ConfigureServices section:

    services.AddDbContext<[original context name]>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")))
    	.AddDbContext<[new Identity context name]>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

    services.AddIdentity<ApplicationUser, IdentityRole>
    //settings (password, lockout, cookie, user)


    ...
    .AddEntityFrameworkStores<[new Identity context name]>()
    ...

    Hope this helps someone else.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 28, 2017 9:45 PM

All replies

  • User-707554951 posted

    Hi muybn

    Your ApplicationContext was inheriting from DbContext? .

     changed it to inherit from IdentityDbContext<ApplicationUser> and it works.

    https://stackoverflow.com/questions/39262222/invalidoperationexception-when-registering-a-new-user-with-asp-net-core-identit

    Best regard

    Cathy

    Friday, June 23, 2017 8:06 AM
  • User1280950372 posted

    Thank you, Cathy. I've read the referenced article and everything else on the subject many times. Did you mean to ask "Is your ApplicationContext inheriting from DbContext?" This is how I presently have the class name and inheritance set up, similar to what the article suggests:

    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>

    Friday, June 23, 2017 3:43 PM
  • User1280950372 posted

    I found a coworker who had had a similar problem and helped me resolve it in the following way. It's similar to Cathy's suggested reference https://stackoverflow.com/questions/39262222/invalidoperationexception-when-registering-a-new-user-with-asp-net-core-identit but that didn't go far enough. Some posts  I've read advise something similar to what I discovered but seemed to indicate that it shouldn't be necessary to create two contexts, but since it worked for me, as far as I'm concerned it was necessary. I created my context by reverse-engineering the database (a whole subject unto itself) then, among other things, made sure it had this signature:

    public partial class [name of your context class] : DbContext
    
    //constructor
    public [context name](DbContextOptions<[context name]> options) : base(options) { }
    protected override void OnModelCreating(ModelBuilder builder)
    {
    //DbSets and classes generated by reverse-engineering
    }

    Next, I created a separate context class for the Identity elements. Do this by creating a class in the same folder as your first context, decide on a name for the new context, and replace the code in the class with the following:

    using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
    using Microsoft.EntityFrameworkCore;
    using [your project name].Models;
    
    namespace [your project name].[your namespace]
    {
        public class [your Identity context name] : IdentityDbContext<ApplicationUser>
    	{
    		public [Identity context](DbContextOptions<[Identity context]> options)
                : base(options)
            {
    		}
    
    		protected override void OnModelCreating(ModelBuilder builder)
    		{
    			base.OnModelCreating(builder);
    			// Customize the ASP.NET Identity model and override the defaults if needed.
    			// For example, you can rename the ASP.NET Identity table names and more.
    			// Add your customizations after calling base.OnModelCreating(builder);
    		}
    	}
    }
    

    Modify your Startup file under the ConfigureServices section:

    services.AddDbContext<[original context name]>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")))
    	.AddDbContext<[new Identity context name]>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

    services.AddIdentity<ApplicationUser, IdentityRole>
    //settings (password, lockout, cookie, user)


    ...
    .AddEntityFrameworkStores<[new Identity context name]>()
    ...

    Hope this helps someone else.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 28, 2017 9:45 PM