Skip to content

Commit 31e0261

Browse files
CopilotwadepickettCopilot
authored
Add migration warning for Identity options that affect EF Core model (#36296)
* Initial plan * Add important note about Identity options and migrations Co-authored-by: wadepickett <10985336+wadepickett@users.noreply.github.com> * Update connection string to use placeholder format Co-authored-by: wadepickett <10985336+wadepickett@users.noreply.github.com> * Apply suggestion from @wadepickett * Remove explanatory comments from code example per repo guidelines Co-authored-by: wadepickett <10985336+wadepickett@users.noreply.github.com> * Apply suggestion from @Copilot Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Apply suggestion from @Copilot Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Simplify IMPORTANT block to only critical warning, move guidance to regular text Co-authored-by: wadepickett <10985336+wadepickett@users.noreply.github.com> * Apply suggestion from @wadepickett * Update aspnetcore/security/authentication/customize-identity-model.md Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update aspnetcore/security/authentication/customize-identity-model.md Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Remove instructions on Identity options for EF Core Removed detailed instructions on applying Identity options for EF Core migrations, including examples of configuration in Program.cs and design-time factory. * Enhance Identity configuration instructions in documentation Reverting and adding back important notes and examples for configuring Identity options during EF Core migrations --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: wadepickett <10985336+wadepickett@users.noreply.github.com> Co-authored-by: Wade Pickett <wpickett@microsoft.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 00d59da commit 31e0261

File tree

1 file changed

+45
-2
lines changed

1 file changed

+45
-2
lines changed

aspnetcore/security/authentication/customize-identity-model.md

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
---
22
title: Identity model customization in ASP.NET Core
3+
ai-usage: ai-assisted
34
author: ajcvickers
45
description: This article describes how to customize the underlying Entity Framework Core data model for ASP.NET Core Identity.
5-
ms.author: tdykstra
6-
ms.date: 10/29/2024
6+
ms.author: wpickett
7+
ms.date: 11/03/2025
78
uid: security/authentication/customize_identity_model
89
---
910
<!-- ms.sfi.ropc: t -->
@@ -39,6 +40,48 @@ When a new app using Identity is created, steps 1 and 2 above have already been
3940

4041
Repeat the preceding steps as changes are made to the model.
4142

43+
> [!IMPORTANT]
44+
> When Identity options that affect the underlying EF Core model are configured (for example, `options.Stores.MaxLengthForKeys` or `options.Stores.SchemaVersion`), those option values must also be applied at design time for EF Core Migrations to generate the correct model shape. If the EF tooling runs without these options configured, generated migrations may omit the intended changes. For more information, see [efcore#36314](https://github.com/dotnet/efcore/issues/36314).
45+
To ensure Identity options are applied consistently during migration generation, use one of the following approaches:
46+
47+
* **Set the startup project:** Run `dotnet ef` commands (or PMC commands) with the application project that calls `AddDefaultIdentity` or `AddIdentityCore` set as the startup project. For example, when running commands from a class library project, specify the startup project with `dotnet ef migrations add {MIGRATION_NAME} --startup-project {PATH_TO_APP_PROJECT}`, where the `{MIGRATION_NAME}` placeholder is the migration name and the `{PATH_TO_APP_PROJECT}` placeholder is the path to the app project.
48+
* **Implement `IDesignTimeDbContextFactory`:** Alternatively, implement an `IDesignTimeDbContextFactory<TContext>` that constructs the context and applies the equivalent Identity option configuration. For an Aspire-friendly solution, see [efcore#35285 (comment)](https://github.com/dotnet/efcore/issues/35285#issuecomment-3161145762).
49+
50+
Example Identity configuration in `Program.cs`:
51+
52+
```csharp
53+
builder.Services
54+
.AddDefaultIdentity<ApplicationUser>(options =>
55+
{
56+
options.Stores.SchemaVersion = IdentitySchemaVersions.Version2;
57+
options.Stores.MaxLengthForKeys = 256;
58+
})
59+
.AddEntityFrameworkStores<ApplicationDbContext>();
60+
```
61+
62+
Example design-time factory:
63+
64+
```csharp
65+
public class DesignTimeApplicationDbContextFactory
66+
: IDesignTimeDbContextFactory<ApplicationDbContext>
67+
{
68+
public ApplicationDbContext CreateDbContext(string[] args)
69+
{
70+
var optionsBuilder = new DbContextOptionsBuilder<ApplicationDbContext>()
71+
.UseSqlServer("{CONNECTION_STRING}");
72+
73+
return new ApplicationDbContext(optionsBuilder.Options);
74+
}
75+
}
76+
```
77+
78+
> [!NOTE]
79+
> You cannot access `options.Stores.MaxLengthForKeys` directly inside `OnModelCreating` because dependency injection isn’t available at design time. Instead, specify the configured value directly (such as `HasMaxLength(256)`), or use a design-time mechanism to pass settings if needed.
80+
> For more details, see <xref:security/authentication/identity-configuration>.
81+
82+
> [!TIP]
83+
> Always verify the resulting model snapshot reflects the intended key lengths or schema version after adding a migration.
84+
4285
## The Identity model
4386

4487
### Entity types

0 commit comments

Comments
 (0)