Cannot create a DbSet for 'IdentityUser' because this type is not included in the model for the context.
means Identity was pointed at a DbContext that has no Identity tables in its model. Make that
context derive from IdentityDbContext<IdentityUser> instead of
DbContext, and keep your own DbSet properties on it.
The registration AddEntityFrameworkStores<AppDb>() compiled and the app
built its services with this plain DbContext, so nothing complained until the
first Identity call asked the model for its user type. The fix in the lab was the base class
plus one extra using line; without that line the build failed.
The error
Thrown from UserManager.CreateAsync in a seed that runs at startup, on the error stream; the top of the stack trace:
Unhandled exception. System.InvalidOperationException: Cannot create a DbSet for 'IdentityUser' because this type is not included in the model for the context.
at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.get_EntityType()
...
at Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserOnlyStore`7.FindByNameAsync(String normalizedUserName, CancellationToken cancellationToken)
at Microsoft.AspNetCore.Identity.UserManager`1.FindByNameAsync(String userName)
at Microsoft.AspNetCore.Identity.UserValidator`1.ValidateUserName(UserManager`1 manager, TUser user)
...
Why it happens
The context in the lab was an ordinary one with a single table of its own:
...
class AppDb(DbContextOptions<AppDb> options) : DbContext(options)
{
public DbSet<Appointment> Appointments => Set<Appointment>();
}
Identity's store asks the context for a set of IdentityUser, and this model only
knows Appointment. The stack shows where it first asked: inside
CreateAsync, the user validator called FindByNameAsync, and the throw
came from InternalDbSet.get_EntityType, a check against the model before any query
was sent. EnsureCreated() ran without error a line earlier, so
the first sign of the mistake was the seed, not the start of the app. The name in quotes,
IdentityUser, is the entity the store asked for, so it also tells you which type
argument the base class needs.
The fix
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
...
class AppDb(DbContextOptions<AppDb> options) : IdentityDbContext<IdentityUser>(options)
{
public DbSet<Appointment> Appointments => Set<Appointment>();
}
...
With the new base class and the extra using line the same seed printed
Seed: Succeeded. The Appointments property stayed in the class; the lab
did not read or write that table. The first attempt at the fix left the using line out
and did not build, so the old program ran again; the evidence keeps that run with a note. A
database that already exists was not tested here.
How it was reproduced
A dotnet new web -f net10.0 project on .NET SDK 11.0.100 with
Microsoft.AspNetCore.Identity.EntityFrameworkCore 10.0.10 and
Microsoft.EntityFrameworkCore.Sqlite 10.0.10, registered with
AddIdentityCore<IdentityUser>().AddEntityFrameworkStores<AppDb>(). A scope
after builder.Build() called EnsureCreated() and then
CreateAsync for one fictional user. AppDb, Appointment and
lab.db are the test's own names. Every version was built with dotnet build
and started with dotnet bin\Debug\net10.0\IdnLab.dll --urls http://127.0.0.1:15620
(no launch profile; the fixed run printed Hosting environment: Production). The first
run had no database file yet, and each later run moved the previous file aside first.
Frequently asked
- Why does Identity say IdentityUser is not included in the model?
- The DbContext passed to AddEntityFrameworkStores derives from DbContext, so its model has no Identity entities. Derive it from IdentityDbContext<IdentityUser> instead.
- Can I keep my own tables in the Identity DbContext?
- In the lab the class kept its DbSet<Appointment> property after the base class changed to IdentityDbContext<IdentityUser>, and the seed succeeded; the Appointments table itself was not read or written.
- Why did EnsureCreated not fail before the error?
- In the lab EnsureCreated ran without error on the plain DbContext, whose model held only the Appointment entity. The first Identity query, inside CreateAsync, was the call that threw.
Registering Identity for the wrong user type gives a different message, covered in Unable to resolve service for type UserManager. More decoded errors in the Fixes category.