Skip to content

Identity: Check/ensure that base.OnModelCreating is called when AppDbContext inherits from IdentityDbContext. #52314

Open
@voroninp

Description

@voroninp

Is there an existing issue for this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe the problem.

It's very easy to forget adding base.OnModelCreating(modelBuilder); call when switching inheritance from DbContext to IdentityDbContext

And as a result, one will get the following exception from EF Core:

System.InvalidOperationException : The entity type 'IdentityUserLogin' requires a primary key to be defined.

IMO, this is a very unfriendly message, because it's not telling how to fix the problem in the first place. Unless you google and find the answer on Stack Overflow. I believe framework should guide with errors, not frighten.

Describe the solution you'd like

An easy solution is to provide an analyzer that will check for the base call, or even an interceptor to fix the issue silently.

The ideal solution for me would be, if C#/CLR allowed implementing Template Method pattern with reusing the name:

class IdentityContext
{
    protected override sealed void OnModelCreating(ModelBuilder modelBuilder)
    {
        ...
        new.OnModelCreating(modelBuilder);
    }
 
    new protected virtual void OnModelCreating(ModelBuilder modelBuilder)
    {
    }
}

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Pillar: Dev ExperienceanalyzerIndicates an issue which is related to analyzer experiencearea-identityIncludes: Identity and providersenhancementThis issue represents an ask for new feature or an enhancement to an existing one

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions